Hello friends
I want to get the cell value of selected row in data grid in wpf and also tell me in which event get this value
How to get the cell value of selected row in data grid in WPF
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Priya LingeRank #785 · 2.9K reputationPosted Sep 18, 2011, 9:14 PM EDT
Please try this.
dataGridFirst.PreparingCellForEdit += new EventHandler
void dataGridFirst_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
Suppose this list(get and set values)
//var myValue = dataGrid1.SelectedItem[0].ToString();
ClientsList selectedrow = ClientsList)dataGridFirst.SelectedItem;
string Clientname = selectedrow.Name;
int Clientid = selectedrow.ClientID;
}
OR
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)(row);
{
DataGrid dataGrid = sender as DataGrid;
if (e.AddedItems!=null && e.AddedItems.Count>0)
{
// find row for the first selected item
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild
// find grid cell object for the cell with index 0
DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
if (cell != null)
{
Console.WriteLine(((TextBlock)cell.Content).Text);
}
}
}
}
static T GetVisualChild(Visual parent) where T : Visual(v);
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
child = GetVisualChild
if (child != null)
break;
}
return child;
}
Hope this will help you.
Thanks.