I got cell value & cell header value from this code. how to get row header value in wpf datagrid for the selected cell?
XAML-
<DataGrid Name="Grid1" Height="550" Width="850" AutoGenerateColumns="True"
CanUserResizeRows="False" CanUserDeleteRows="False"
CanUserAddRows="False" AreRowDetailsFrozen="False"
SelectionUnit="Cell" SelectedCellsChanged="Grid1_SelectedCellsChanged"
CellEditEnding="Grid1_CellEditEnding" LoadingRow="Grid1_LoadingRow">
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="LightYellow"/>
</Style>
</DataGrid.Style>
</DataGrid>
CODE -
How i manipulate column header is -
private DataTable _GameData;
public Grid()
{
_GameData = new DataTable();
_GameData.Columns.Add(new DataColumn("Sunday", typeof(string)));
_GameData.Columns.Add(new DataColumn("Monday", typeof(string)));
_GameData.Columns.Add(new DataColumn("Tuesday", typeof(string)));
_GameData.Columns.Add(new DataColumn("Wednesday", typeof(string)));
_GameData.Columns.Add(new DataColumn("Thursday", typeof(string)));
_GameData.Columns.Add(new DataColumn("Friday", typeof(string)));
_GameData.Columns.Add(new DataColumn("Saturday", typeof(string)));
for(int i=0;i<=23;i++)
{
var row = _GameData.NewRow();
_GameData.Rows.Add(row);
row["Sunday"] = "";
row["Monday"] = "";
row["Tuesday"] = "";
row["Wednesday"] = "";
row["Thursday"] = "";
row["Friday"] = "";
row["Saturday"] = "";
}
InitializeComponent();
Grid1.ItemsSource = _GameData.AsDataView();
Grid1.RowHeaderWidth = 50;
Grid1.ColumnWidth = 100;
}
public DataTable GameData
{ get { return _GameData; } }
How i manipulate row header-
private void Grid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
var id = e.Row.GetIndex();
switch (id)
{
case 0:
{
e.Row.Header = "12-00";
break;
}
case 1:
{
e.Row.Header = "12-30";
break;
}
case 2:
{
e.Row.Header = "1-00";
break;
}
case 3:
{
e.Row.Header = "1-30";
break;
}
case 4:
{
e.Row.Header = "2-00";
break;
}
case 5:
{
e.Row.Header = "2-30";
break;
}
case 6:
{
e.Row.Header = "3-00";
break;
}
case 7:
{
e.Row.Header = "3-30";
break;
}
case 8:
{
e.Row.Header = "4-00";
break;
}
case 9:
{
e.Row.Header = "4-30";
break;
}
case 10:
{
e.Row.Header = "5-00";
break;
}
case 11:
{
e.Row.Header = "5-30";
break;
}
case 12:
{
e.Row.Header = "6-00";
break;
}
case 13:
{
e.Row.Header = "6-30";
break;
}
case 14:
{
e.Row.Header = "7-00";
break;
}
case 15:
{
e.Row.Header = "7-30";
break;
}
case 16:
{
e.Row.Header = "8-00";
break;
}
case 17:
{
e.Row.Header = "8-30";
break;
}
case 18:
{
e.Row.Header = "9-00";
break;
}
case 19:
{
e.Row.Header = "9-30";
break;
}
case 20:
{
e.Row.Header = "10-00";
break;
}
case 21:
{
e.Row.Header = "10-30";
break;
}
case 22:
{
e.Row.Header = "11-00";
break;
}
case 23:
{
e.Row.Header = "11-30";
break;
}
default:
{
break;
}
}
Got Column Header & Cell value -
private void Grid1_SelectedCellsChanged(object sender,
SelectedCellsChangedEventArgs e)
{
foreach (var item in e.AddedCells)
{
var col = item.Column as DataGridColumn;
MessageBox.Show(""+col.Header);
var fc = col.GetCellContent(item.Item);
if (fc is TextBlock)
{
MessageBox.Show("Values" + (fc as TextBlock).Text);
}
}
}
Output - message show the selected column header value, Arrow Explain the Row header want to show.
You need to get DataGridRow
first to access its Header
.
DataGridRow you can get using ItemContainerGenerator.ContainerFromItem() method of sender dataGrid like this:
private void Grid1_SelectedCellsChanged(object sender,
SelectedCellsChangedEventArgs e)
{
DataGrid dg = (DataGrid)sender;
foreach (var item in e.AddedCells)
{
DataGridRow row =
(DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(item.Item);
var col = item.Column as DataGridColumn;
MessageBox.Show("" + col.Header);
var fc = col.GetCellContent(item.Item);
if (fc is TextBlock)
{
MessageBox.Show("Values" + (fc as TextBlock).Text);
}
MessageBox.Show("Row Header " + row.Header.ToString());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With