WPF-DataGridTemplate Column of DataGrid is not available in code behind when trying to access with DataRowView

krishna 466 Reputation points
2020-10-12T06:57:54.843+00:00

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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andy ONeill 361 Reputation points
    2020-10-12T10:17:09.613+00:00

    This is because a datarowview is data rather than controls.

    You will find no controls in there at all.

    Casting to checkbox ( ui ) will just get you null.

    Assuming this is the correct property you're using then it should be a bool which will be bound to the IsChecked property of the checkbox in the UI.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. krishna 466 Reputation points
    2020-10-12T10:32:23.367+00:00

    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);
                }
            }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.