C# datagridview Columns clear on sort

时间:2018-06-04 17:44:35

标签: c# datagridview issue-tracking gridview-sorting

        var ds = new DataSet();
        ds.ReadXml(XMLFile);

        DataGridLogView.DataSource = ds.Tables["Header"];
        DataGridLogView.DataMember = "Data";
        DataGridLogView.Columns.Add("5", "Record #");
        DataGridLogView.Columns.Add("6", "Record");

The columns "5" & "6" are the problem columns that i add after my datasource loads in. I can sort any column besides the two i created(5 & 6) but when i sort any of the columns, the two columns i create will clear of all data i add and i cannot seem to figure out why it is doing this. Thanks in advance!

2 个答案:

答案 0 :(得分:0)

将DataGridView控件绑定到外部数据源时,非数据绑定单元格在排序时不会自动保留。您添加的列不在数据集中,因此它们会被清除。如果您的列“5”和“6”是从行数据派生的,您可以尝试在DataGridView.RowsAdded事件中填充它们。或者,您可以在DataGridView.Sorted事件中重新加载这些列。

答案 1 :(得分:0)

       datatable.Columns.Add("Record #", typeof(string));
       datatable.Columns.Add("Record", typeof(string));

在设置datagridviews数据源之前,我必须将列添加到数据集中,然后排序才能完美进行。