【原】winform定制datagrid模板

   由于工作需要跟winform下的datagrid,打了下交道,winform下的datagrid跟web中的结构差别很大,搞得很是焦头烂额,存档备用![b]ps:vs2003环境[/b]
    想要使用自定义的列样式,首先必须了解DataGrid的结构。在我们将一个DataGrid控件拖到窗体上的时候,我们可以马上在属性中设置它的样式,改变它数据行的交替色等。这不同于ASP.NET下的样式设置。在ASP.NET中,最终的改变都体现为网页的HTML语言。而在WinForm下,这种改变是通过控制DataGridDataGridTableStyle属性(对象)来体现的,而DataGridTableStyle又是由若干个DataGridColumnStyle组成的, 微软默认提供了两种列样式:DataGridTextBoxColumnDataGridBoolColumn.
      
    
 1   /// <summary>
 2        /// 设置dataGrid1的模板
 3        /// </summary>
 4        /// <param name="dt"></param>

 5          private   void  SetGridStyle(DataTable dt)
 6          {
 7         
 8            if(!dataGrid1.TableStyles.Contains(dt.TableName))
 9            {
10                DataGridTableStyle dts=new DataGridTableStyle();
11                dts.MappingName=dt.TableName;    //注意:必须加上这一句,否则自定义列格式无法使用
12                dataGrid1.TableStyles.Add(dts);                            
13                dataGrid1.TableStyles[0].GridColumnStyles.Clear();
14                dts.AlternatingBackColor=System.Drawing.Color.FromName("Lavender");
15                dts.HeaderBackColor=System.Drawing.Color.FromName("MidnightBlue");
16                dts.HeaderForeColor=System.Drawing.Color.FromName("White");                        
17                dts.GridLineColor=System.Drawing.Color.GhostWhite;
18                
19                //========================设置表头栏位===========================    
20                DataGridTextBoxColumn colFileName1 = new DataGridTextBoxColumn();
21                colFileName1.Width=160;                
22                colFileName1.MappingName = "FileName";
23                colFileName1.HeaderText = "文件名";
24                colFileName1.Alignment=HorizontalAlignment.Center;
25                dts.GridColumnStyles.Add(colFileName1);                 
26
27                DataGridTextBoxColumn colRecordCount = new DataGridTextBoxColumn();
28                colRecordCount.Width=75;
29                colRecordCount.HeaderText = "记录数"
30                colRecordCount.Alignment=HorizontalAlignment.Center;
31                colRecordCount.MappingName = "RecordCount";
32                dts.GridColumnStyles.Add(colRecordCount); 
33                
34                DataGridTextBoxColumn colFileRecordCount = new DataGridTextBoxColumn();
35                colFileRecordCount.Width=100;
36                colFileRecordCount.HeaderText = "文件记录数";
37                colFileRecordCount.MappingName = "FileRecordCount";
38                colFileRecordCount.Alignment=HorizontalAlignment.Center;
39                dts.GridColumnStyles.Add(colFileRecordCount);
40
41                DataGridTextBoxColumn colState = new DataGridTextBoxColumn();
42                colState.Width=65;
43                colState.HeaderText = "完成状态";
44                colState.MappingName = "state"
45                colState.Alignment=HorizontalAlignment.Center;
46                dts.GridColumnStyles.Add(colState); 
47 
48                DataGridTextBoxColumn colAddDate = new DataGridTextBoxColumn();
49                colAddDate.Width=143;
50                colAddDate.ReadOnly=true;
51                colAddDate.HeaderText = "添加时间";
52                colAddDate.Alignment=HorizontalAlignment.Center;
53                colAddDate.Format="yyyy-MM-dd hh:mm:ss";                  
54                colAddDate.MappingName = "AddDate";
55                dts.GridColumnStyles.Add(colAddDate);
56
57                dts.RowHeadersVisible=false;//隐藏自动生成的左边列
58                dts.AlternatingBackColor= System.Drawing.Color.FromName("Lavender");              
59                dataGrid1.TableStyles.Add(dts);
60               
61                    
62            }

你可能感兴趣的:(datagrid)