I have a WPF application I am working on. I am using a DataGrid control to display a fields list with various information. I cannot find the DatGridView control in my Visual Studio Community toolbox. Majority of my Google searches only bring back info for DataGridView, very frustrating. I would use DataGridView if I could find, but I digress. What I want to be able to do is capture the current row when a user selects a cell, so I can get a value from the adjacent cell. I am having no luck finding out how to do this. Here is how I create my DataGrid:
private void DisplayFieldLengths(string strFLFileName, Int32 intTotalRowSize, int[] intFieldLengths)
{
int intDisplayCnt, colCnt = 0;
string strData = "";
lblFLInfo.Content = "File: " + strFLFileName;
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Size", typeof(string));
dt.Columns.Add(" New Size", typeof(string));
dt.Columns[0].ReadOnly = true;
dt.Columns[1].ReadOnly = true;
for (intDisplayCnt = 0; intDisplayCnt < intFieldLengths.Length; intDisplayCnt++)
{
strData = "Field" + (intDisplayCnt + 1) + "|" + intFieldLengths[intDisplayCnt].ToString() + "|1";
dt.Rows.Add(strData.Split('|'));
}
dtGrid.ItemsSource = dt.DefaultView;
lblRowSize.Content = "Total row length: " + intTotalRowSize;
}
Here is the xaml for the DataGrid.
<DataGrid IsReadOnly="False" Name="dtGrid" Loaded="GridLoaded" CurrentCellChanged="NewFieldSizeChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Height="365" Margin="48,53,0,0" Width="272" BorderThickness="1" BorderBrush="Black">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#FF9DF3D6" />
<Setter Property="Foreground" Value="#000000" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>