C#通用重写dataGrid标题函数

         private   void  Form1_Load( object  sender, System.EventArgs e)
        
{
            DataTable dt 
= new DataTable("test");
            dt.Columns.Add(
"col1",typeof(string));
            DataColumn dc 
= new DataColumn("sys_id",typeof(int));
            dc.AutoIncrement 
= true;
            dc.AutoIncrementSeed 
= 1;
            dc.AutoIncrementStep 
= 1;
            dt.Columns.Add(dc);
            dt.Columns.Add(
"col2",typeof(string));
            DataColumn dc2 
= new DataColumn("col3",typeof(Boolean));
            dc2.DefaultValue 
= false;
            dt.Columns.Add(dc2);
            dt.Columns.Add(
"col4",typeof(decimal));
            dt.Columns.Add(
"col5",typeof(System.DateTime));

            DataRow dr 
= dt.NewRow();
            dr[
"col1"= "test";
            dr[
"col2"= "col2";
            
try
            
{
                dr[
"col3"= true;
            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

            dr[
"col4"= 1.2;
            dr[
"col5"= DateTime.Today;
            dt.Rows.Add(dr);

            DataRow dr2 
= dt.NewRow();
            dr2[
"col1"= "test";
            dr2[
"col2"= "col2";
            dr2[
"col3"= false;
            dr2[
"col4"= 5;
            dr2[
"col5"= DateTime.Now;
            dt.Rows.Add(dr2);

            dataGrid1.DataSource 
= dt;

            
string[] col_map,col_name;
            col_map 
= new string[6]{"col1","col2","col3","sys_id","col4","col5"};
            col_name 
= new string[6]{"测试一","测试二","正确/错误","系统编号","金额","时间"};
            set_styles(dataGrid1,col_map,col_name);
        }

        
private   void  set_styles(DataGrid dg, string [] col_map, string [] col_name)
        
{
            DataGridTableStyle dts 
= new DataGridTableStyle();
            DataTable dt 
= null;
            
int num_col =0;
            
string str_db_type;
            
int li_i;
            
if(dg.DataSource is DataView)
            
{
                dt 
= (dg.DataSource as DataView).Table;
            }

            
else if(dg.DataSource is DataTable)
            
{
                dt 
= dg.DataSource as DataTable;
            }

            
else if(dg.DataSource is DataSet)
            
{
                dt 
= (dg.DataSource as DataSet).Tables[0];
            }

            dts.MappingName 
= dt.TableName;
            num_col 
= col_map.Length;
            DataGridTextBoxColumn dgbc;
            DataGridBoolColumn dgbl;
            
for(li_i = 0;li_i<num_col;li_i++)
            
{
                str_db_type 
= dt.Columns[col_map[li_i].ToString().Trim()].DataType.ToString();
                
//MessageBox.Show(str_db_type);
                if(str_db_type == "System.Boolean")
                
{
                    dgbl 
= new DataGridBoolColumn();
                    dgbl.HeaderText 
= col_name[li_i];
                    dgbl.MappingName 
= col_map[li_i];
                    dts.AlternatingBackColor 
= Color.Teal;
                    dts.GridColumnStyles.Add(dgbl);
                }

                
else
                
{
                    dgbc 
= new DataGridTextBoxColumn();
                    dgbc.HeaderText 
= col_name[li_i];
                    dgbc.MappingName 
= col_map[li_i];
                    dgbc.Alignment 
= HorizontalAlignment.Center;
                    dts.AlternatingBackColor 
= Color.Teal;
                    dts.GridColumnStyles.Add(dgbc);
                }

            }

            dg.TableStyles.Add(dts);
        }


        
private   void  button1_Click( object  sender, System.EventArgs e)
        
{
            DataTable dt;
            dt 
= dataGrid1.DataSource as DataTable;
            DataRow dr 
= dt.NewRow();
            dt.Rows.Add(dr);
        }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值