I have a DataGrid which I fill with data from my sql database.
Now I want to get a specific cell (2nd cell) from the row I selected with a checkbox.
This is what I have now:
WPF XAML:
C# code:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
DataGrid row = (DataGrid)myGrid.SelectedItems[1];
System.Windows.MessageBox.Show(row);
}
I get a error when running:
System.ArgumentOutOfRangeException
{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}
What am I doing wrong? Do I have to use another approach to get the cell I want?
解决方案
I finally found it! Very simple.
DataRowView drv = (DataRowView)myGrid.SelectedItem;
String result = (drv["CustomerID"]).ToString();
MessageBox.Show(result);