Search code examples
c#wpfdatagrid

How to return number of selected rows in datagrid to textbox in WPF?


I would like to select multiple rows (not necessarily in any particular order) and return number of selected rows to the textbox.

Something like: You have selected 'x' rows.

I was trying to use this kind of method:

private void SelectedRows(object sender, SelectionChangedEventArgs e)
{
    var selectedRows = SomeDataGrid.SelectedRows.Count.ToString();
    RowCount.Text = "You have selected " + selectedRows + "rows";
}

but to no avail. There was an error saying that DataGrid do not contain SelectedRow definition.


Solution

  • The DataGrid does not have a SelectedRows property (if you're using Visual Studio, IntelliSense should have warned you about this). Instead, use the property SelectedItems.