I'm using WPF and .NET 4.0. Recently in one of my programs I switched from using ListView with GridView to DataGrid.
I want to be able to select and highlight the whole row like I was able to do in ListView.
In ListView, when I click on the empty space right from the last column, I'm still able to select the row. The whole row is highlighted, not only the cells.
In DataGrid however, after setting SelectionMode="Single" and SelectionUnit="FullRow", the row is selectable only when I click on any cell in it, not in the empty space right to the last column.
How can I use the highlighting behavior from ListView here?
WPF DataGrid (SfDataGrid) allows you to select one or more rows or cells. For selecting specific row or group of rows you have to set SelectionUnit as Row and for selecting a specific cell or group of cells you have to set SelectionUnit as Cell or Any. In SelectionUnit.
You can access the currently selected item of a DataGrid using the SelectedItem property. After the first line you need to extract the information (e.g. some Id) from the item in order to delete it in the database. Usually you cast the SelectedItem to the object you used to bind to the grid. See also this response.
There are two solutions:
I personally prefer the first solution; it's cleaner than the second one.
There is one more solution if you can use code behind in your project. You can handle mouse down event of datagrid and programmatically select the clicked row:
private void SomeGridMouseDown(object sender, MouseButtonEventArgs e)
{
var dependencyObject = (DependencyObject)e.OriginalSource;
//get clicked row from Visual Tree
while ((dependencyObject != null) && !(dependencyObject is DataGridRow))
{
dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
}
var row = dependencyObject as DataGridRow;
if (row == null)
{
return;
}
row.IsSelected = true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With