简体   繁体   English

C#datagridview列按类别清除

[英]C# datagridview Columns clear on sort

        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. 列“ 5”和“ 6”是我在数据源加载后添加的问题列。除了创建的两个列(5和6)之外,我还可以对任何列进行排序,但是当我对任何列进行排序时,两列创建将清除我添加的所有数据,而且我似乎无法弄清为什么这样做。 Thanks in advance! 提前致谢!

With a DataGridView control bound to an external data source, non-data bound cells are not automatically persisted when sorting. 通过将DataGridView控件绑定到外部数据源,排序时不会自动保留未绑定数据的单元格。 Your added columns are not in the dataset, so they get cleared. 您添加的列不在数据集中,因此将其清除。 If your columns "5" and "6" are derived from the row data you might try filling them in the DataGridView.RowsAdded event. 如果列“ 5”和“ 6”是从行数据派生的,则可以尝试在DataGridView.RowFyre事件中填充它们。 Alternatively you could reload those columns in the DataGridView.Sorted event. 或者,您可以在DataGridView.Sorted事件中重新加载这些列。

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

I had to add the columns to the dataset before setting the datagridviews datasource then the sorting worked perfect. 在设置datagridviews数据源之前,我必须将列添加到数据集中,然后排序才能完美进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM