Click here to Skip to main content
15,887,421 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:

XML-file:
<Window x:Class="BootstrapGenerator.WindowDataGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BootstrapGenerator"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Title="WindowDataGrid" Height="450" Width="800">
    <Grid Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="85*" />
            <ColumnDefinition Width="15*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30*" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>

        <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" >
        </DataGrid>

        <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" />

    </Grid>
</Window>



C-code-file

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()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // this wil display row, otherwise column+content 
            // TRY COMMENT AND UNCOMMENT NEXT LINE
            // 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;
            _dataTable.Columns.Add(colID);

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


            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);
                }
                _dataTable.Rows.Add(myNewRow);
            }

            return _dataTable;
        }

    }
}
Posted
Comments
[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