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
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Priya LingePosted Sep 19, 2011, 1:14 AM
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.