Sorting DataGrid when datasource is a DataSet
-
I have a MyDataSet : DataSet class which contains 3 talbes and 2 data relations. This is set to a custom datagrid which can expand to show something like Users -> tickets for user -> details of ticket. I'm sure this is entirely simple, but how do I sort while keeping my dataset as the datasource? When I google they always discard the dataset and do something like:
MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = dv; _dgUsers.DataBind();
This is useless as I've just lost my dataset. I tried modifying the view of the table inside the dataset but it just gets ignored (although it remains set when I sort again):MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = **ds**; _dgUsers.DataBind();
How should it be done? When I clone the DataSet it seems to work. Why is that? -
I have a MyDataSet : DataSet class which contains 3 talbes and 2 data relations. This is set to a custom datagrid which can expand to show something like Users -> tickets for user -> details of ticket. I'm sure this is entirely simple, but how do I sort while keeping my dataset as the datasource? When I google they always discard the dataset and do something like:
MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = dv; _dgUsers.DataBind();
This is useless as I've just lost my dataset. I tried modifying the view of the table inside the dataset but it just gets ignored (although it remains set when I sort again):MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = **ds**; _dgUsers.DataBind();
How should it be done? When I clone the DataSet it seems to work. Why is that? -
Perhaps you should try: ds.Tables[0].DefaultView.Sort = "something ASC"; _dgUsers.DataSource = ds; _dgUsers.DataBind(); Ben
That's what I did in the second code block. Doesn't matter if you touch the dg.DataSource or set it to a local variable as its a pointer to the same dataset object. Like I said, I would see "something ASC" set the next time I would sort. The code was there to toggle so clicking it you could see it going from ASC to DESC and back again. However, it did nothing. For some reason we needed to clone the dataset and set it back to the datagrid.