The below code is my WPF DataGrid XAML. I try to access the data grid template column with checkbox in code behind
<DataGrid x:Name="wdgVinpaymentDetail" AutoGenerateColumns="False" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" CanUserDeleteRows="True" Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={local1:RatioConverter}, ConverterParameter='0.4' }" CanUserAddRows="False" LoadingRow="WdgVinpaymentDetail_LoadingRow" Loaded="WdgVinpaymentDetail_Loaded" IsReadOnly="False" >
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="btnDeleteVin">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="DeleteVIN" IsEnabled="True" Click="DeleteVIN_Click"
Command="Delete"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="Select" Header="Select" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="chkSelectAll" Checked="ChkSelectAll_Checked" Unchecked="ChkSelectAll_Unchecked" IsChecked="{Binding IsChecked}"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkSelect" Click="ChkSelect_Click" ></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Sl. No." Width ="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.04' }" x:Name="RALNSLNO" Binding="{Binding RALNSLNO}" IsReadOnly="True" />
<DataGridTextColumn Header="VIN" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.12' }" x:Name="RALNVIN" Binding="{Binding RALNVIN}" IsReadOnly="True" />
<DataGridTextColumn Header="NSP" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.08' }" x:Name="RALNUAMT" Binding="{Binding RALNUAMT}" IsReadOnly="True" />
<DataGridTextColumn Header="VATamt" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.04' }" x:Name="VATamt" Binding="{Binding VATamt}" IsReadOnly="True" />
<DataGridTextColumn Header="VAT%" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.04' }" x:Name="VATper" Binding="{Binding VATper}" IsReadOnly="True" />
<DataGridTextColumn Header="OthExpn" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="TotalOthExpn" Binding="{Binding TotalOthExpn}" IsReadOnly="True" />
<DataGridTextColumn Header="OthExpnvat" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="TotalOthExpnvat" Binding="{Binding TotalOthExpnvat}" IsReadOnly="True" />
<DataGridTextColumn Header="Total" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="Total" Binding="{Binding Total}" IsReadOnly="True" />
<DataGridTextColumn Header="Hoprice" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="Hoprice" Binding="{Binding HoPrice}" IsReadOnly="True" />
<DataGridTextColumn Header="Rpdno" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="Rpdno" Binding="{Binding Rpdno}" IsReadOnly="True" />
<DataGridTextColumn Header="Rpdprice" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="Rpdprice" Binding="{Binding Rpdprice}" IsReadOnly="True" />
<DataGridTextColumn Header="Errflg" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="Errflg" Binding="{Binding Errflg}" IsReadOnly="True" />
<DataGridTextColumn Header="Ptag" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="RALNRFNO2" Binding="{Binding RALNRFNO}" IsReadOnly="True" />
<DataGridTextColumn Header="Model Code" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="STMOCD" Binding="{Binding STMOCD}" IsReadOnly="True" />
<DataGridTextColumn Header="Character Code" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="STCHCD" Binding="{Binding STCHCD}" IsReadOnly="True" />
<DataGridTextColumn Header="Model Year" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="STMOYR" Binding="{Binding STMOYR}" IsReadOnly="True" />
<DataGridTextColumn Header="Manufacturing Year" Width = "{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={local1:RatioConverter}, ConverterParameter='0.07' }" x:Name="STMFYR" Binding="{Binding STMFYR}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
n my code behind i try to access the select checkbox
private void WdgVinpaymentDetail_Loaded(object sender, RoutedEventArgs e)
{
try
{
foreach (DataRowView dr in wdgVinpaymentDetail.Items)
{
CheckBox chk = dr.Row[1] as CheckBox;
checkboxes.Add(chk);
}
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
However the first two datagridtemplatecolumns are not available in datarowview with this code when i use this dr.Row[1] i get the fourth value VIN ,dr[0] is Sl.No