在Datagrid C#中显示列名

时间:2012-12-20 08:28:33

标签: c# winforms

我有一个为dataview创建datagrid的代码:

        DataTable dt = new DataTable();
        DataRow dr;
        DataGridTableStyle tableStyle = new DataGridTableStyle();
        dataGrid1.TableStyles.Clear();
        tableStyle.MappingName = dt.TableName;
        DataGridTextBoxColumn columnStyle;
        dt.Columns.Add(new DataColumn(" Description - Code ", typeof(string)));
        dt.Columns.Add(new DataColumn("Found", typeof(string)));
        dt.Columns.Add(new DataColumn("Expected", typeof(string)));
        dt.Columns[0].MaxLength = 240;
        DataColumn dataColumn = dt.Columns[0];
        dataColumn.Caption=" Description - Code ";
        columnStyle = new DataGridTextBoxColumn();
        columnStyle.Width = 210;
        columnStyle.MappingName = dt.Columns[0].ColumnName;
        tableStyle.GridColumnStyles.Add(columnStyle);
        DataGridTextBoxColumn cs;
        cs = new DataGridTextBoxColumn();
        cs.MappingName = dt.Columns[1].ColumnName;
        cs.Width = 35;
        DataGridTextBoxColumn cs2;
        cs2 = new DataGridTextBoxColumn();           
        cs2.MappingName = dt.Columns[2].ColumnName;
        cs2.Width = 35;
        tableStyle.GridColumnStyles.Add(cs);
        tableStyle.GridColumnStyles.Add(cs2);
        dataGrid1.TableStyles.Add(tableStyle);
        for (int j = 0; j < distL.Count; j++)          
        {
            dr = dt.NewRow();
            dr[0] = distL[j];
            dr[1] = a[j] + 1;
            dr[2] = QuantExp[j];
            dt.Rows.Add(dr);
         }
        DataView dv = new DataView(dt).Table.DefaultView;
        dv.BeginInit();          
        dataGrid1.RowHeadersVisible = true;          
        return dv;

我的问题是,当生成数据网格时,每个列的名称在网格中都不可见。有谁知道原因?

1 个答案:

答案 0 :(得分:0)

好的,我发现了......

要查看列名,必须放入“columnStyle.MappingName”“columnStyle.HeaderText”