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
{
public partial class WindowDataGrid : Window
{
public WindowDataGrid()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
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));
colID.AutoIncrement = true;
colID.AutoIncrementSeed = 1;
colID.AutoIncrementStep = 1;
colID.ReadOnly = true;
_dataTable.Columns.Add(colID);
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;
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;
}
}
}