Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a WPF application. I have a textbox in which I give input which is saved to database on button click, adjacently I have a Datagrid present which will populate the data in database. Now when I add new data it gets stored in database but is not reflected on datagrid.
I guess I will have to refresh the datagrid so I used DataGrid.Items.Refresh() but this does not work for my application.
Plz help how to solve this issue.
Thanks

Code Snippet:

C#
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
//Here is the code to save the data to XML
}


My datagrid gets bind as follows in wpf .xaml file :
XML
<Window.Resources>
        <XmlDataProvider Source="C:\Output.xml"  
                         XPath="Data" x:Key="DataFile" />
    </Window.Resources>

<DataGrid AutoGenerateColumns="False" Height="177" HorizontalAlignment="Left" Margin="322,62,0,0" Name="dgData" VerticalAlignment="Top" Width="149" ItemsSource="{Binding Source={StaticResource DataFile},XPath=*}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Type" Binding="{Binding XPath=@Type}" />
                <DataGridTextColumn Header="Expense" Binding="{Binding XPath=@Value}" />
            </DataGrid.Columns>
        </DataGrid>


My XML looks as follows :
XML
<Data>
   <Record>
     <Type>
       ABC
     </Type>
     <Expense>
       125
     </Expense>
   </Record>
   <Record>
     <Type>
       DEF
     </Type>
     <Expense>
       250
     </Expense>
   </Record>
</Data>


I just want to update the grid after the data is added to xml .
Posted
Updated 21-Jun-12 2:53am
v4
Comments
Jim Jos 21-Jun-12 6:55am    
You need to refresh the collection used for itemsource not the datagrid.
sagar wasule 21-Jun-12 7:40am    
can you please explain a bit more
Sandeep Mewara 21-Jun-12 8:32am    
Share the related code snippet that you are using. As JinJos already pointed, it is collection you need to refresh.

All your business entities must implement INotifyPropertyChanged interface, for UI notification.
Second, for beter performance use ObservableCollection<> class as item source object, in your dataGrid.
Third, in Xaml set to TextBox binding mode to follow : Mode=TwoWay.
Hope this will help.
 
Share this answer
 
(I am assuming WPF, but you have both WPF and ASP.NET)

Because you are using a static resource, there is no way to notify the system when you update the list. This means that the list pretty much has to be in place before you load the form. I would guess that this is your problem. Another option would be to have a instantiation of a class that will serve as the ViewModel. When the data gets updated, create a new list (have to have a change to have INotifyPropertyChange work, otherwise the PropertyChange will just be ignored).
 
Share this answer
 
v3

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