Prabhakaran
Prabhakaran

Reputation: 1337

How to get the row index of a WPF Datagrid?

I am using image control column in wpf datagrid. That control is used to delete the row in datagrid if clicked.Can any one tell me how to bubble the controls click event to select the entire row of the grid. Below is my code as of now.

XAML Code :

  <DataGrid x:Name="dg" >
  <DataGrid.Columns>
 <DataGridTextColumn Header="ID"  Binding="{Binding Path=ZoneId}" />
<DataGridTextColumn Header="Sector" Binding="{Binding Path=SectorIds"/>
  <DataGridTemplateColumn Width="40">
     <DataGridTemplateColumn.CellTemplate >
              <DataTemplate>                                     
       <Image x:Name="imgDel"   Source="Delete.png" Stretch="None" MouseLeftButtonDown="imgDel_MouseLeftButtonDown" />
             </DataTemplate>                                    
            </DataGridTemplateColumn.CellTemplate>
           </DataGridTemplateColumn>
     </DataGrid.Columns>
    </DataGrid>

Code Behind :

private void imgDel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{            
       var inx = dg.SelectedIndex;
}

My requirement is when I clicked the image control in the row , it should delete the entire row from datacontrol. My data grid is binded with a collection.

Thanks

Upvotes: 0

Views: 3856

Answers (3)

Manish Basantani
Manish Basantani

Reputation: 17509

I have a utility method that I use to get Grid row/column.

public static Tuple<DataGridCell, DataGridRow> GetDataGridRowAndCell(DependencyObject dep)
{
    // iteratively traverse the visual tree
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return null;

    if (dep is DataGridCell)
    {
        DataGridCell cell = dep as DataGridCell;

        // navigate further up the tree
        while ((dep != null) && !(dep is DataGridRow))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        DataGridRow row = dep as DataGridRow;

        return new Tuple<DataGridCell, DataGridRow>(cell, row);
    }

    return null;
}

Could be called like this:

private void OnDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DependencyObject dep = (DependencyObject)e.OriginalSource;

            Tuple<DataGridCell, DataGridRow> tuple = GetDataGridRowAndCell(dep);
        }

Upvotes: 1

Andy
Andy

Reputation: 6466

If you're trying to get a reference to the DataGridRow object that your image is inside you could use the VisualTreeHelper to find it.

private void imgDel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{            
     DependencyObject dataGridRow = sender as DependencyObject;
     while (dataGridRow != null && !(dataGridRow is DataGridRow)) dataGridRow = VisualTreeHelper.GetParent(dataGridRow);
     if (dataGridRow!= null)
     {
           // dataGridRow now contains a reference to the row,
     }    
}

Upvotes: 0

Sadique
Sadique

Reputation: 22841

You have the sender you can use it to get your row index.

Upvotes: 1

Related Questions