Search code examples
c#wpfdatagridrowcell

How to get the contents of the first cell of a row when double clicked on any column of a row in C# DataGrid


I want to get the contents of the first cell of any row when any cell in a row is double clicked. (Example if I click on cell[row1, column2] or cell[row1, column3] in both cases I want to get the contents of the cell[row1, column1]).

With the code I have written I get the contents of the cell that is double clicked.

In my code var cellValue is giving me the contents of the cell. So, I want to achieve the contents of the cell[row r, column1] inside var cellValue and private List<DataGrid> m_AllDgTag = new List<DataGrid>();

My Code:

private void DgCM_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    int currentPnl = 0;
    if (m_AllDgTag.Count > 1)
    {
       currentPnl = m_CurrentFDS;
    }
    var cellInfo = m_AllDgTag[currentPnl].CurrentCell;

    if (cellInfo != null && m_CurrentStep != null)
    {
       var column = cellInfo.Column as DataGridBoundColumn;

       int cellColIndex = cellInfo.Column.DisplayIndex;

       if (column != null)
       {

           var element = new FrameworkElement() { DataContext = cellInfo.Item };
           BindingOperations.SetBinding(element, FrameworkElement.TagProperty, 
               column.Binding);
           var cellValue = element.Tag;

          ...
       }
    }
}

I am attaching the image of the Item Array. I am able to get the data of the row inside 'var rowinfo'. From here i want to get the data of the column [0]. So in this case i want to get the value for example column[0] value: F21B1C001.enter image description here


Solution

  • I found the solution to the problem. If we use this piece of code we will get the value of column [0] of any row clicked. var cellValue = ((System.Data.DataRowView)cellInfo.Item).Row.ItemArray[0];

    Thank you for the help @Gustavo Oliveira