简体   繁体   中英

Custom sort xml bound DataGridView

I have a DataGridView bound to xml file. I would like to sort by first column and treat values as integers (not strings).

XmlDataDocument xml = new XmlDataDocument();
xml.DataSet.ReadXml("file.xml");

dataGridView.DataSource = new BindingSource(xml.DataSet, "Item");
  • Sort(IComparer) doesn't work
  • Sort(DataGridViewColumn,ListSortDirection) treats all values as strings
  • SortCompare event isn't triggered

What do I have to do to make this work? Inherit DataGridView and override Sort? Sort BindingSource? Sort DataSet?

Note to future readers:

Accepted answer doesn't work for my code snippet. DataType must be changed before data is loaded. DataSet should be manually created:

DataTable table = new DataTable("Item");
table.Columns.Add(new DataColumn("id", typeof(int)));
table.Columns.Add(new DataColumn("name", typeof(string)));

DataSet set = new DataSet();
set.Tables.Add(table);
set.ReadXml("file.xml", XmlReadMode.IgnoreSchema);

dataGridView.DataSource = new BindingSource(set, "Item");

您需要更改DataSet,以使列的DataType为System.Int32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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