Reputation: 75
I'm having trouble returning which row a button was clicked from in my wpf project.
I have the following so far...
XAML -
<DataGrid x:Name="myDataGrid" HorizontalAlignment="Left" Width="550">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"></DataGridTextColumn>
<DataGridTemplateColumn Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="RowEvent">X</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C# -
private void RowEvent(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
}
I seem to recall that when I was using a dataGridView in Win Forms I could get the row index from e.RowIndex if I remember correctly, but I cant seem to do that this time.
Could anyone point me in the right direction?
Upvotes: 4
Views: 4912
Reputation: 176
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
myDataGrid.GetColumn(btn);
myDataGrid.GetRow(btn);
MessageBox.Show(myDataGrid.GetColumn(btn).ToString());
MessageBox.Show(myDataGrid.GetRow(btn).ToString());
}
This is how i managed it! I just used the click event of the button
Upvotes: 1
Reputation: 2222
Just use:
DataGrid.SelectedIndex Property
... like this:
private void RowEvent(object sender, RoutedEventArgs e)
{
int index = myDataGrid.SelectedIndex;
MessageBox.Show(index.ToString());
}
Upvotes: 3