数据网格列重复 [英] Data Grid columns duplicating

查看:65
本文介绍了数据网格列重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试使用以下代码从数据集表中实际填充数据网格; -



Hi I am trying to populate a data grid pragmatically from a data set table using the code below ;-

private void WOTest_Load(object sender, EventArgs e)
        {
            FormDataSet = woutil.GetDataset;
            dgComments.DataSource = FormDataSet.Tables["PREWODOC_COMMENTS"];
            foreach (DataColumn col in FormDataSet.Tables["PREWODOC_COMMENTS"].Columns)
            {
                dgComments.Columns.Add(col.ColumnName, col.ColumnName);
            }
        }



此代码确实在数据网格中创建了列,但是没有我想要的4列我得到8列,因为列是重复的。如何停止重复的列我已经执行了代码,添加列的调用只进行了4次,因此我不明白为什么有8列,因为只添加了4个?


This code does create the columns in the data grid however instead of having the 4 columns I was expecting I get 8 columns because the columns are duplicated. How can I stop the columns duplicating I have stepped the code and the call to add the columns is only done 4 times as I would expect so I do not understand why there are 8 columns as there are only 4 added?

推荐答案

您已经在设计区域中绑定了列,因此在代码中您将再次添加它们。只需注释掉添加它们的foreach循环。
You have the columns already bound in the design area and so in code you are adding them again. Just comment out the foreach loop that adds them.


我没有意识到自动生成列属性默认设置为true,在添加列之前将其设置为false已解决了问题。 br $>


I was unaware that the auto generate columns attribute was set to true by default setting this to false before adding the columns has solved the issue.

private void WOTest_Load(object sender, EventArgs e)
{
    FormDataSet = woutil.GetDataset;
    dgComm.DataSource = FormDataSet.Tables["PREWODOC_COMMENTS"];
    dgComm.AutoGenerateColumns = false;
    foreach (DataColumn col in FormDataSet.Tables["PREWODOC_COMMENTS"].Columns)
    {
        dgComm.Columns.Add(col.ColumnName, col.ColumnName);
    }
}


这篇关于数据网格列重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆