none
Get current row for selected cell in vb.net WPF dataGridView

    Question

  • I am needing to get the row index from my dataGridView. I can use .selectedIndex only when the entire row is highlighted. I need to allow the user to select individual cells. When I change the datagrid selection unit to "cell", the selectedIndex property always returns -1. How can I get the index of the row where the cell is selected in this situation?
    Tuesday, July 10, 2012 12:23 PM

Answers

  • Here is the answer on how to do this in VB.net

       

    PrivateSubdgPrices_SelectedCellsChanged(sender AsObject, e AsSystem.Windows.Controls.SelectedCellsChangedEventArgs) HandlesdgPrices.SelectedCellsChanged

    Dimi AsDataRowView= CType(e.AddedCells(0).Item, DataRowView)  

    Dimd AsString= i.Item(0).ToString

    'for test

    MsgBox(dgDataSet.Tables(0).Rows.IndexOf(CType(i.Row, DataRow)))

    EndSub       

           


       

    • Marked as answer by 06143831 Tuesday, July 10, 2012 3:27 PM
    Tuesday, July 10, 2012 3:26 PM

All replies

  • You can use code converter for VB.
    DataGridCell cell = DataGridHelper.GetCell(datagrid.SelectedCells[0]);
    var selectedCell = DataGridHelper.GetRowIndex(cell).ToString();

    Helper Class:

    public static class DataGridHelper
        {
            public static DataGridCell GetCell(DataGridCellInfo dataGridCellInfo)
            {
                if (!dataGridCellInfo.IsValid)
                {
                    return null;
                }
    
                var cellContent = dataGridCellInfo.Column.GetCellContent(dataGridCellInfo.Item);
                if (cellContent != null)
                {
                    return (DataGridCell)cellContent.Parent;
                }
                else
                {
                    return null;
                }
            }
            public static int GetRowIndex(DataGridCell dataGridCell)
            {
                // Use reflection to get DataGridCell.RowDataItem property value.
                PropertyInfo rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem", BindingFlags.Instance | BindingFlags.NonPublic);
    
                DataGrid dataGrid = GetDataGridFromChild(dataGridCell);
    
                return dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null));
            }
            public static DataGrid GetDataGridFromChild(DependencyObject dataGridPart)
            {
                if (VisualTreeHelper.GetParent(dataGridPart) == null)
                {
                    throw new NullReferenceException("Control is null.");
                }
                if (VisualTreeHelper.GetParent(dataGridPart) is DataGrid)
                {
                    return (DataGrid)VisualTreeHelper.GetParent(dataGridPart);
                }
                else
                {
                    return GetDataGridFromChild(VisualTreeHelper.GetParent(dataGridPart));
                }
            }
        }


    • Edited by AAKoken Tuesday, July 10, 2012 1:01 PM
    Tuesday, July 10, 2012 1:00 PM
  • Doesn't work. Can't cast value of type System.Windows.DependencyObject to Type DataGridCell. Tried casting every way possible.
    Tuesday, July 10, 2012 1:08 PM
  • C# or VB ? If you convert the code to VB, some errors usually occur.
    Tuesday, July 10, 2012 1:20 PM
  • I done the conversion from C# to VB. first error I got was from statement: dim dataGrid as DataGrid = GetDataGridFromChild(dataGridCell)

    The error is a casting issue. it says the value of DataGridCell can't be converted to DependencyObject. I tried doing a direct cast and ctype(DependencyObject)

    I could fix the rest of them if I could figure out how to make this cast work.


    • Edited by 06143831 Tuesday, July 10, 2012 2:06 PM
    Tuesday, July 10, 2012 1:34 PM
  • Here is the answer on how to do this in VB.net

       

    PrivateSubdgPrices_SelectedCellsChanged(sender AsObject, e AsSystem.Windows.Controls.SelectedCellsChangedEventArgs) HandlesdgPrices.SelectedCellsChanged

    Dimi AsDataRowView= CType(e.AddedCells(0).Item, DataRowView)  

    Dimd AsString= i.Item(0).ToString

    'for test

    MsgBox(dgDataSet.Tables(0).Rows.IndexOf(CType(i.Row, DataRow)))

    EndSub       

           


       

    • Marked as answer by 06143831 Tuesday, July 10, 2012 3:27 PM
    Tuesday, July 10, 2012 3:26 PM