【问题标题】:DevExpress Grid view Query - DataSource property set to DataTable but showing incorrect number of ColumnsDevExpress 网格视图查询 - DataSource 属性设置为 DataTable 但显示的列数不正确
【发布时间】:2013-05-10 23:44:06
【问题描述】:

我有一个用于绑定到我的 DevExpress GridView 的 DataTable。

我的 DataTable 是动态的,即 - 有时它可以带来 4 列,有时甚至 20 列。问题是,当我第一次使用具有 20 列的 dataTable 设置我的 GridView 时,它将正确显示,下一步时间如果我的 DataTable 只有 4 列,我的 GirdView 仍将显示所有 20 列,其值仅在我的 DataTable 当时具有的 4 列中。

我该如何解决这个问题?

附上截图。

这与清除某些内存实例/处置对象有关吗?如果是这样,请帮助使用 DevExpress GridView 的 API。

更新

此代码调用设置数据源

bindingSource1.DataSource = dtBindToGridView; // MyDataTable
gcAnalysisTaskPermission.DataSource = bindingSource1; //My GridView 
bindingSource1.ResetBindings(true); // Reset the BindingSource

谢谢,

曼格什

【问题讨论】:

    标签: winforms data-binding datatable devexpress


    【解决方案1】:

    首先尝试如下重置数据源:

    grid.DataSource = null;
    grid.DataSource = GetData();
    

    如果你有绑定源,这不起作用

    致电gridView.PopulateColumns()

    【讨论】:

      【解决方案2】:

      如果您使用的是BindingSource,请尝试设置ResetBinding()

      public void SetData()
      {
         //setting the datasource of your binding source
         myBindingSource.DataSource = GetMyDataTable(); 
      
         //after the datasource has been set, call this.
         //use true, because the metadata has changed
         myBindingSource.ResetBindings(true); 
      }
      

      更新

      根据DevExpress,您可以尝试拨打PopulateColumns

      public void SetData()
      {
         //setting the datasource of your binding source
         myBindingSource.DataSource = GetMyDataTable(); 
      
         gridView1.PopulateColumns();
      }
      

      【讨论】:

      • bindingSource1.DataSource = dtBindToGridView; // MyDataTable gcAnalysisTaskPermission.DataSource = bindingSource1; //我的GridView bindingSource1.ResetBindings(true); // 重置 BindingSource
      • 您可以在设置数据源后隐藏所有空白列。但它是一个丑陋的解决方案......
      • @JensKloster 谢谢!!您更新的答案解决了我的问题:)
      猜你喜欢
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2011-08-05
      相关资源
      最近更新 更多