views:

204

answers:

1

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");
+1  A: 

You need to change the DataSet so that the column's DataType is System.Int32.

SLaks