Sorting DataViews

  • Thread starter Thread starter Terry McNamee
  • Start date Start date
T

Terry McNamee

DataView d = new DataView(state.getPlayers());

d.Sort = "Name ASC";


foreach(DataRow dr in d.Table.Rows)

{

dataMenu.Items.Add(new
DataItem(dr["Name"].ToString(),dr["File"].ToString()));

}



Please could someone tell me why this isn't sorting the names in
alphabetical order?



Many thanks
 
foreach(DataRow dr in d.Table.Rows)

This line is the problem. You are using a DataView to sort, yet you are accessing the underlying table directly.
In order to use the sorted data, you must query the table *through* the DataView you created:

DataView view = new DataView(state.getPlayers());
view.Sort = "Name";

foreach (DataRowView row in view)
// Notice how the view is enumerates "DataRowView" objects
{
dataMenu.Items.Add(new DataItem(row["Name"].ToString(), row["File"].ToString()));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top