如何刷新/重新加载WPF数据网格中的所有项目 [英] How to refresh/ reload all the items in a WPF datagrid

查看:305
本文介绍了如何刷新/重新加载WPF数据网格中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从sql数据库中获取详细信息并在datagrid中显示它们,如下所示。

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:


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" ><!--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">



当用户通过编辑此数据网格的选定行对数据库进行任何更改时,必须使用最近的数据表填充数据网格。



我能够克et correct datatable,但当它作为Itemsource给出datagrid时,它没有显示最近的那个而不是显示早期的。



当我关闭应用程序并重新启动它时,它的显示正确的。



我尝试了什么:



我试过DataGrid.Items.Clear();但是收到错误当ItemsSource正在使用时,操作无效。请改为使用ItemsControl.ItemsSource访问和修改元素。



我试过DataGrid.Items.refresh ();和Datagrid.UpdateLayout(),但没有任何工作。



如果有办法实现它,请告诉我。



提前谢谢


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

推荐答案

假设你(正确)绑定到 ObservableCollection ,您可以尝试调用 OnCollectionChanged 方法。



另一方面,它应该自动发生在集合中的项目已更新/删除/插入。在我看来,您的商品不是来自 INotifyPropertyChanged ,或者您的商品不在 ObservableCollection 中,或者两个。



FWIW,我*永远不会*将DataTable或DataSet直接绑定到UI控件。我创建了一个由viewmodel加载的模型,并将viewmodel绑定到控件。
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.


再次将您更新的itemssource再次设置为 -
just asign your updated itemssource again -
DG_FollowUp.ItemsSource = dt.DefaultView;


< DataGridTextColumn Binding ={Binding [TicketID / Subject], Mode = TwoWay,UpdateSourceTrigger = PropertyChanged }标题=TicketID / SubjectWidth =300CanUserSort =False/>
<DataGridTextColumn Binding="{Binding [TicketID/Subject],Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="TicketID/Subject" Width="300" CanUserSort="False"/>


这篇关于如何刷新/重新加载WPF数据网格中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆