开发者

Sorting DataTable with WPF DataGrid?

Okay, this is driving me nuts. I've spent hours trying to figure out what should be a simple solution, but I'm ha开发者_如何转开发ving no luck.

I have a [WPF Toolkit] DataGrid on an XAML page that has a DataTable as its ItemsSource. I also have a button on my page that gets the DataGrid's SelectedIndex (selected row) and uses it as a variable in a function that reads the bound DataTable's row at that index and returns a value. Everything works fine until I click a column header to sort it. It sorts the DataGrid but does not sort the DataTable with it, so my SelectedIndex has changed but the index of the DataTable has not, thus it returns the wrong value.

I've looked for Column Header click events - no luck; I tried to get the header of the column by which the grid is currently sorted - nada; I tried to use a "Click" EventSetter on the DataGridTextColumn template - not supported.

I'm completely at a loss. If WPF is supposed to be an improvement over Windows Forms, why has some of the simple functionality been removed? (It's also dumb that you have to bind data just to add rows, just saying.) I can use a Windows Forms DataGrid and won't have any trouble, but then I can't style it.

Maybe I'm not performing the check properly or something...? Below is my retrieval/output code. Anyone have any ideas??? Your help will be greatly appreciated!

DataRow selectedRow = my_data.Tables[0].Rows[my_grid.SelectedIndex];
MessageBox.Show(selectedRow["ItemName"]);


The sorting is applied to the DefaultView of the datatable.So it will not be applied to the Datatable directly.To access the sorted table use

DataTable.DefaultView.ToTable()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜