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
Wpf: Cannot Reuse Window After It Has Been Closed
"Nested Foreach" VS "Lambda/Linq Query" Performance(Linq-To-Objects)
In C# Why Can't a Conditional Operator Implicitly Cast to a Nullable Type
The Remote Server Returned an Unexpected Response: (413) Request Entity Too Large
How to Format 07/03/2012 to March 7Th,2012 in C#
Passing Parameters to Xslt Stylesheet via .Net
When Is Using the C# Ref Keyword Ever a Good Idea
Find If Point Lies on Line Segment
Output Console.Writeline from Wpf Windows Applications to Actual Console
Linq: from a List of Type T, Retrieve Only Objects of a Certain Subclass S
Entity Framework Creates a Plural Table Name, But the View Expects a Singular Table Name
Removing Hidden Characters from Within Strings
Why am I Getting Sehexception When Calling Roleenvironment.Getconfigurationsettingvalue("Mykey")