Click here to Skip to main content
15,882,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I get from Wpf DataGrid the Row, Column and Cellvalue on a click or a select event?
Is this so difficult?
I have almost the solution but not at the same time
If I put the DataGrid in SelectionUnit="Cell" I get the column and cellvalue
If I put the DataGrid in DataGridSelectionUnit.FullRow; I get the Row
But not at the same time !!!

What I have tried:

<Window x:Class="BootstrapGenerator.WindowDataGrid"
        Title="WindowDataGrid" Height="450" Width="800">
    <Grid Margin="5">
            <ColumnDefinition Width="85*" />
            <ColumnDefinition Width="15*" />
            <RowDefinition Height="30*" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />

        <DataGrid x:Name="dataGridLayout" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="1" Grid.RowSpan="5" Margin="0,0,0,0" AutoGenerateColumns="True" 
                  SelectionMode="Single" SelectedCellsChanged="dataGridLayout_SelectedCellsChanged" SelectionUnit="Cell"  IsReadOnly="True"
                  AlternatingRowBackground="LightGray" >

        <Label Grid.Column="1" Grid.Row="1" Name="lblRow" Content="Row" VerticalAlignment="Center" />
        <Label Grid.Column="1" Grid.Row="2" Name="lblColumn" Content="Column" VerticalAlignment="Center" />
        <Label Grid.Column="1" Grid.Row="3" Name="lblContent" Content="Content" VerticalAlignment="Center" />



using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;

namespace BootstrapGenerator
    /// <summary>
    /// Interaction logic for WindowDataGrid.xaml
    /// </summary>
    public partial class WindowDataGrid : Window
        public WindowDataGrid()

        private void Window_Loaded(object sender, RoutedEventArgs e)
            // this wil display row, otherwise column+content 
            // dataGridLayout.SelectionUnit = DataGridSelectionUnit.FullRow;
            dataGridLayout.ItemsSource = CreateDataTable().DefaultView;

        private void dataGridLayout_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
            foreach (var item in e.AddedCells)
                var col = item.Column as DataGridColumn;
                var fc = col.GetCellContent(item.Item);
                lblColumn.Content = string.Format("Column: {0}", col.DisplayIndex);
                if (fc is TextBlock)
                    lblContent.Content = string.Format("Content: {0}", (fc as TextBlock).Text);
            if (sender != null)
                DataGrid grid = sender as DataGrid;
                if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                    var rowview = grid.SelectedItem as DataRowView;
                    if (rowview != null)
                        DataRow row = rowview.Row;
                        lblRow.Content = string.Format("Row: {0}", row.ItemArray[0].ToString());

        public static DataTable CreateDataTable()
            DataTable _dataTable = new DataTable("Layout");

            DataColumn colID = new DataColumn("ID", typeof(int));  // Create and add a ID column
            colID.AutoIncrement = true;
            colID.AutoIncrementSeed = 1;
            colID.AutoIncrementStep = 1;
            colID.ReadOnly = true;

            // Create and add Custom columns
            DataColumn colWork = new DataColumn("Col1", typeof(String));
            colWork = new DataColumn("Col2", typeof(String));
            colWork = new DataColumn("Col3", typeof(String));
            colWork = new DataColumn("Col4", typeof(String));
            colWork = new DataColumn("Col5", typeof(String));
            colWork = new DataColumn("Col6", typeof(String));
            colWork = new DataColumn("Col7", typeof(String));
            colWork = new DataColumn("Col8", typeof(String));
            colWork = new DataColumn("Col9", typeof(String));
            colWork = new DataColumn("Col10", typeof(String));

            DataRow myNewRow;  // Add rows
            for (int i = 0; i < 30; i++)
                myNewRow = _dataTable.NewRow();
                for (int c = 1; c <= 10; c++)
                    myNewRow[c] = string.Format(" R:{0} C:{1} ", i, c);

            return _dataTable;

[no name] 27-Apr-21 13:49pm    
You're able to retrieve the cell's underlying data row (which you're doing); the "ID" identifies the relative row (+1) and can otherwise be used as a key. You have everything you need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900