0

I am facing an issue with loading a datagrid. Actually, I am showing a datagrid by populating a datatable filled with values fetched from SQL DB with 3 extra columns having checkboxes. When user clicks on one checkbox, the next check box have to be enabled and is to be updated in DB. For achieving this, I did like this. When user clicks on one check box, it will update datatable that first task was completed, so next time when it read db,it enables next one by disabling first. I did every thing but am struck with clearing the earlier datgrid and showing updated one. DB was getting updated and even in itemsource, i am updating the updated one. but still its showing the previous one.

XAML CODE OF MY DATAGRID:

<DataGrid Name="DG_FollowUp" ItemsSource="{Binding dt}" AutoGenerateColumns="False" SelectionMode="Single" SelectedIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="01" BorderBrush="Black" BorderThickness="0.5" LoadingRow="DG_FollowUp_LoadingRow" IsEnabled="True" IsReadOnly="True" AlternationCount="2" ><!--VirtualizingStackPanel.IsVirtualizing="True"-->
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding EntryID}" Header="Entry ID" Width="75">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="HorizontalAlignment" Value='Center'/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding [TicketID/Subject]}" Header="TicketID/Subject" Width="300" CanUserSort="False"/>
                    <DataGridTextColumn Binding="{Binding [AddDate]}" Header="Start Date" Width="90" CanUserSort="False"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding [Status]}" Header="Status" Width="80" CanUserSort="False"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding [OwnerShip]}" Header="Ownership" Width="130" CanUserSort="False"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding [Comments]}" Header="Comments" Width="300" CanUserSort="False"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding [SLA DATE]}" Header="SLA Date" Width="90" CanUserSort="False"></DataGridTextColumn>                        
                    <DataGridTemplateColumn Header="Strike1" Width="65">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Click="CHK_Strike1_Click" IsChecked="{Binding Checked}" Name="CHK_Strike1" HorizontalAlignment="Center"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Strike2" Width="65">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Click="CHK_Strike2_Click" IsChecked="{Binding Checked}" Name="CHK_Strike2" HorizontalAlignment="Center"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Strike3" Width="65">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Click="CHK_Strike3_Click" Name="CHK_Strike3" IsChecked="{Binding Checked}"  HorizontalAlignment="Center"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Close Tckt" Width="95">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Click="Chk_CloseTckt_Click" Name="Chk_CloseTckt" IsChecked="{Binding Checked}"  HorizontalAlignment="Center" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>                

CODE for showing updated db to DataGrid:

using (SqlConnection con = new SqlConnection(UserIDDataTableConnString))
            {
                SqlCommand cmd = new SqlCommand("select * from TBL_TicketMngr where Status = 'Follow Up';", con);
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable("TBL_TicketMngr");
                sda.Fill(dt);
                ////DG_FollowUp.Items.Clear();
                ////DG_FollowUp.ItemsSource = null;

                //DG_FollowUp.ItemsSource = dt.DefaultView;
                ////DG_FollowUp.Items.Refresh();
                DG_FollowUp.DataContext = dt.DefaultView;
                DG_FollowUp.ItemsSource = null;

                //DG_FollowUp.ItemsSource = dt.DefaultView;

                foreach (DataRow row in dt.Rows)
                {
                    ThreeStrikeStatus.Add(row["StrikeStage"].ToString());
                }
                for (int i = 0; i < ThreeStrikeStatus.Count; i++)
                {
                    if (ThreeStrikeStatus[i] == "ONE")
                    {
                        DG_FollowUp.GetCell(i, 7).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 7).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 9).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 9).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 10).IsEnabled = false;
                    }
                    else if (ThreeStrikeStatus[i] == "TWO")
                    {
                        DG_FollowUp.GetCell(i, 7).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 7).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 8).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 8).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 10).IsEnabled = false;
                    }
                    else if (ThreeStrikeStatus[i] == "THREE")
                    {
                        DG_FollowUp.GetCell(i, 7).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 7).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 8).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 8).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 9).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 9).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 10).IsEnabled = true;
                    }
                    else if (ThreeStrikeStatus[i] == "")
                    {
                        DG_FollowUp.GetCell(i, 8).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 8).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 9).IsEnabled = false;
                        DG_FollowUp.GetCell(i, 9).Visibility = Visibility.Hidden;
                        DG_FollowUp.GetCell(i, 10).IsEnabled = false;
                    }
                }
                DG_FollowUp.UpdateLayout();

please let me know, if there is any mistake in the above one or if there is any other way to achieve the required functionality.

I am also facing a issue with checking the checkbox in datagridtemplatecolumn based on the value obtained from db. Please help me if you have any solution for that.

1 Answers1

0

When using a Binding method, you shouldn't be using refresh, or reloading your DataGrid data. The whole point of binding is to implement INotifyPropertyChanged and let the delegate update your UI for you.

Here is a link which shows how to implement INotifyPropertyChanged

How to implement DataTable property with INotifyPropertyChanged

and what I would personally do for a DataTable is to put that in a CollectionViewSource and bind to it.

ViewModel

public class ViewModel
{
    //Properties
    CollectionViewSource _CollectionViewSource { get; set; }
    DataTable dt { get; set; }
    
    //Constructor
    public ViewModel() 
    {
        _CollectionViewSource.Source = dt.DefaultView
    } 
}

XAML

<DataGrid ItemsSource="{Binding _CollectionViewSource.View}" />
Community
  • 1
  • 1
Adam Vincent
  • 3,281
  • 14
  • 38