【问题标题】:Refreshing DevExpress.XtraGrid after adding items to list that was boun to it将项目添加到列表后刷新 DevExpress.XtraGrid
【发布时间】:2012-03-29 19:17:28
【问题描述】:

我有一个List<SomeClass> 绑定到 DevExpressXtraGrid,例如:

MyXtraGrid.DataSource = MyList;

我在 XtraGrid 设计器中制作了一些列。一切正常,行显示在网格中,但是当我将对象添加到 MyList 时,网格没有刷新,也没有显示新项目。

我尝试过MyXtraGrid.Refresh(),尝试重新绑定MyXtraGrid.DataSource = MyList,但没有成功。

MyXtraGrix.MyView.PopulareColumns() 不是一个选项,因为我不会将 MyList 中的所有字段都显示在网格中,这会听到我使用设计器配置的列。

如何刷新网格视图以显示我添加的对象?

【问题讨论】:

    标签: c# binding devexpress xtragrid


    【解决方案1】:

    只需这样做:

        private void BindCollection(IEnumerable collection)
        {
            // keep current index
            GridView view = MyXtraGrid.Views[0] as GridView;
            int index = 0;
            int topVisibleIndex = 0;
            if (view != null)
            {
                index = view.FocusedRowHandle;
                topVisibleIndex = view.TopRowIndex;
            }
    
            MyXtraGrid.BeginUpdate();
            MyXtraGrid.DataSource = collection;
            MyXtraGrid.RefreshDataSource();
    
            if (view != null)
            {
                view.FocusedRowHandle = index;
                view.TopRowIndex = topVisibleIndex;
            }
    
            MyXtraGrid.EndUpdate();
        }
    

    你也可以获取选中的行,在新的数据源设置好后重新选中。

    还请注意,您可以使用BindingList<> 代替List,以便让网格自行更新,而无需编写一行代码。阅读更多here

    【讨论】:

      【解决方案2】:

      使用GridControl.RefreshDataSource Method,因为我正在使用我的集合数据源是某个类的列表,它包含另一个类的列表以创建主视图详细信息。

      GridControl scheduleGrid = sender as GridControl;
      MyXtraGrid.DataSource = collection;
      scheduleGrid.RefreshDataSource();
      

      如果您对 IList(网格外)进行更改,我相信您 然后必须调用 RefreshDatasource 方法和 IList 不做更改通知。 RefreshDataSource Method

      如果你愿意,我相信你应该从IBindingList 继承 这一切都自行啮合在一起。否则我确实相信 RefreshDatasource 应该可以工作。

      参考:
      Refreshing Grid When Using Custom Enumerator
      How to keep unchanged scroll position when refreshing grid data_
      Filtering the Object DataSource

      【讨论】:

        猜你喜欢
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-30
        • 2011-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多