Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fetching details from a sql database and am showing them in datagrid as shown below.
C#
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:
HTML
<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" ><!--VirtualizingStackPanel.IsVirtualizing="True"-->
                    <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
Posted
Updated 2-Feb-17 9:24am
v2
Comments
[no name] 2-Feb-17 9:15am    
DataBind the data grid's itemsource.
VamsiPenta 2-Feb-17 9:35am    
I tried as below, But it doesnt worked.
XAMl: <DataGrid Name="DG_FollowUp" ItemSource={Binding}/>
.cs: DG_FollowUp.DataContext = dt.DefaultView;
but it didnt worked.

public partial class MainWindow : MetroWindow, INotifyPropertyChanged
I tried writing like this, even then its not working
[no name] 2-Feb-17 9:44am    
Am I supposed to know what "not worked" means? Did you set your data context? Did you create a property to bind to? Did you debug your application to find out what "not work" means?
VamsiPenta 2-Feb-17 9:51am    
When i am setting datacontext to it, its showing empty datgrid and at same time i am not getting any error while debugging
[no name] 2-Feb-17 9:53am    
Then fix your binding.

just asign your updated itemssource again -
DG_FollowUp.ItemsSource = dt.DefaultView;
 
Share this answer
 
Comments
CHill60 2-Feb-17 10:55am    
Where would they put that? Surely binding would be the way forward? (Genuine question, I'm still getting up to speed with some aspects of WPF)
johannesnestler 2-Feb-17 11:01am    
What I meant is he should just re-run his query and asign the result as ItemsSource again (the only way I know to "update" in this scenario). But I would never follow such an approach - I use MVVM and bind ObservableCollections to the DataGrid - so updating the underlaying data is enough. So don't bind DataTables directly...
VamsiPenta 2-Feb-17 11:14am    
Actually i am new to WPF. As I know only this way, I proceeded with this. If there is any other way to achieve the above thing.please let me know so that i can proceed that way
johannesnestler 2-Feb-17 11:34am    
there is no "Problem" if you do it this way - just tight coupling. Anyway just re-run your query on your Refresh-(button/Event/whatever) and re-assign the itemssource. If you are new to WPF it won't take long that you "meet" the MVVM pattern somehow - so I'd recommend to read a bit about it to decide if it is what you want...
CHill60 2-Feb-17 11:16am    
Thanks for that
Assuming you're (correctly) binding to an ObservableCollection, you could try calling the OnCollectionChanged method.

On the other hand, it should automagically happen when the items in the collection are updated/deleted/inserted. It should to me like either your items are not derived from INotifyPropertyChanged, or your items are not in an ObservableCollection, or both.

FWIW, I *NEVER* directly bind a DataTable or DataSet to a UI control. I create a model that is loaded by a viewmodel, and bind the viewmodel to the control.
 
Share this answer
 
v3
<DataGridTextColumn Binding="{Binding [TicketID/Subject],Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="TicketID/Subject" Width="300" CanUserSort="False"/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900