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>
In 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
With the help of @Andy post i did the following corrections,i got an extra column CHKSEL as false while fetching data from database . Then my xaml was changed like this
<DataGridTemplateColumn x:Name="Select" Header="Select" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="chkSelectAll" Checked="ChkSelectAll_Checked" Unchecked="ChkSelectAll_Unchecked" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkSelect" IsChecked="{Binding CHKSEL}" ></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The datatable has false in CHKSEL and the IsChecked attribute is binded with CHKSEL ,so at load time it is always uncheked
In my code behind checked and unchecked statement ,i get the datatable which was binded to itemsource property of my grid,change the CHKSEL value to true in datatable and then set ItemSource to updated datatable,for unchecked function set value to false in dattable and then bind
private void ChkSelectAll_Checked(object sender, RoutedEventArgs e)
{
try
{
CheckBox checkbox = (CheckBox)e.OriginalSource;
if (checkbox.IsChecked == true)
{
DataTable ldtDetails = (DataTable)App.Current.Properties["CancelDetail"];
for(int i = 0; i <= ldtDetails.Rows.Count - 1; i++)
{
ldtDetails.Rows[i][0] = "true";
}
wdgVinpaymentDetail.ItemsSource = null;
wdgVinpaymentDetail.ItemsSource = ldtDetails.DefaultView;
App.Current.Properties["CancelDetail"] = ldtDetails;
}
e.Handled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ChkSelectAll_Unchecked(object sender, RoutedEventArgs e)
{
try
{
CheckBox checkbox = (CheckBox)e.OriginalSource;
if (checkbox.IsChecked == false)
{
DataTable ldtDetails = (DataTable)App.Current.Properties["CancelDetail"];
for (int i = 0; i <= ldtDetails.Rows.Count - 1; i++)
{
ldtDetails.Rows[i][0] = "false";
}
wdgVinpaymentDetail.ItemsSource = null;
wdgVinpaymentDetail.ItemsSource = ldtDetails.DefaultView;
App.Current.Properties["CancelDetail"] = ldtDetails;
}
e.Handled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}