简体   繁体   中英

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

Hasan, after your comment i understand that your code is a WPF app.

I create a sample in WPF

this is the XAML of the grid, the key of the solution is the

DataGrid.Resources Style TargetType="DataGridRow"

<Grid>
    <DataGrid HorizontalAlignment="Left" 
              Height="203" 
              Margin="175,126,0,0"
              DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}" 
              ItemsSource="{ Binding Persons }" 
              VerticalAlignment="Top" 
              Width="378" >
        <DataGrid.Resources>
            <Style TargetType="DataGridRow">
                <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="LastName" Binding="{Binding LastName}"/>
            <DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

this is the code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
    {
        var dbClickedRow = (DataGridRow)sender;
        MessageBox.Show(((Person)dbClickedRow.DataContext).Name);
    }

    public List<Person> Persons
    {
        get
        {
            return new List<Person>
            {
                new Person{ Name = "Gustavo", LastName = "Oliveira", Age = 35 },
                new Person{ Name = "Another", LastName = "Person", Age = 23 },
                new Person{ Name = "Neymar", LastName = "Junior", Age = 28 },
            };
        }
    }
}

public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM