I am fetching details from a sql database and am showing them in datagrid as shown below.
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.ItemsSource = dt.DefaultView;
}
XAML of datagrid:
<DataGrid Name="DG_FollowUp" 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" ><!--
<datagrid.columns>
<DataGridTextColumn Binding="{Binding [TicketID/Subject]}" Header="TicketID/Subject" Width="300" CanUserSort="False"/>
<DataGridTextColumn Binding="{Binding [AddDate]}" Header="Start Date" Width="90" CanUserSort="False">
<DataGridTextColumn Binding="{Binding [Status]}" Header="Status" Width="80" CanUserSort="False">
<DataGridTextColumn Binding="{Binding [OwnerShip]}" Header="Ownership" Width="130" CanUserSort="False">
<DataGridTextColumn Binding="{Binding [Comments]}" Header="Comments" Width="300" CanUserSort="False">
<DataGridTextColumn Binding="{Binding [SLA DATE]}" Header="SLA Date" Width="90" CanUserSort="False">
When user make any changes in db by editing a selected row of this datagrid, the datagrid has to be filled with recent datatable.
I am able to get correct datatable, but when it is given as Itemsource to datagrid, Its not showing recent one instead showing earlier one.
When I close the application and restart it, its showing correct one.
What I have tried:
I tried DataGrid.Items.Clear(); but am getting an error "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
I tried DataGrid.Items.refresh(); and Datagrid.UpdateLayout(), but nothing are working.
Please let me know if there is a way to achieve it.
Thanks in advance