Wpf Datagrid Get Selected Cell Value

WPF Datagrid Get Selected Cell Value

Please refer to the DataGrid Class page on MSDN. From that page:

Selection

By default, the entire row is selected when a user clicks a cell in a DataGrid, and a user can select multiple rows. You can set the SelectionMode property to specify whether a user can select cells, full rows, or both. Set the SelectionUnit property to specify whether multiple rows or cells can be selected, or only single rows or cells.

You can get information about the cells that are selected from the SelectedCells property. You can get information about cells for which selection has changed in the SelectedCellsChangedEventArgs of the SelectedCellsChanged event. Call the SelectAllCells or UnselectAllCells methods to programmatically select or unselect all cells. For more information, see Default Keyboard and Mouse Behavior in the DataGrid Control.

I have added links to the relevant properties for you, but I'm out of time now, so I hope you can follow the links to get your solution.

Get selected cell value from DataGrid Cell

Try with

Selected Event

, i think u won't have any problems.

Or if u want to keep it in

SelectionChanged

you have to know that it fires when your DataGrid loses focus. So in that case u have

DataGrid.SelectedCells=null;

Good practice will be to test if your

DataGrid.SelectedCells.Coun>0

then continue or return if it don't

WPF Datagrid Get Cell Value in MVVM architecture

The data grid is bound to a collection of users, so each user is represented by one row, not one cell. That's why CurrentCell returns a DataGridCellInfo

If you want the user, bind to CurrentItem instead.

In XAML:

<DataGrid 
ItemsSource="{Binding Users}"
CurrentItem="{Binding SelectedUser}"...

In the view model:

private user selectedUser;
public user SelectedUser
{
get => selectedUser;
set
{
var u = value as user;
selectedUser = u;
}
}

Get value from selected row in WPF Datagrid

If you are showing both XAML and cs code then only we can find the proper solution. Now I am assuming that you are displaying the contents by using binding from an observable collection of any class type. So you can easily get the ID field by,

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
ClassName classObj = dataGridName.SelectedItem as ClassName;
string id = classObj.ID;
}

WPF : Get index of clicked / selected cell on DataGrid

DataGrid x = (DataGrid)this.FindName("myDataGrid");
var index = x.SelectedIndex;

There are also other usefull properties:

x.CurrentColumn;
x.CurrentItem;
x.SelectedItem;
x.SelectedValue;


Related Topics



Leave a reply



Submit