【问题标题】:Binding GridView to dynamic DataTable将 GridView 绑定到动态 DataTable
【发布时间】:2014-12-22 09:43:36
【问题描述】:

我有一个 GridView,我将它绑定到一个在运行时填充的 DataTable。列数和行数不固定。 DataTable 填满后,我将 GridView 的绑定源设置为这个数据表:

 Me.User_infoDTBindingSource.DataSource = User_infoDT

现在的问题是,网格视图根本没有显示任何列或行。 我用过:

Me.UserListGridView.PopulateColumns(User_infoDT)

现在我得到了 GridView 中的所有列,但是我在 DataTable 中设置的列标题都丢失了。 GridView 中的行仍然为空。我还使用了OptionView.EnableAppearanceEvenRow,所以我可以看到在我的 GridView 中生成的行数(应该是这样),但我仍然看不到任何数据。

我已经使用相同的数据源(数据表)在 Windows DataGridView 和 TreeList 中实现了相同的代码,并且运行良好。但是在这里我找不到任何不获取行的原因。

知道这些行在哪里吗?当我将绑定源的数据源设置为数据表时,我调试了代码并检查了 DataTable。当时 GridView.DataRowCount 和 GridView.Columns.Count 是正确的。但是网格视图仍然没有在行中显示任何数据。

更新: 我现在已经删除了绑定源并将 GridView 的数据源直接设置为 DataTable,它工作得很好!!我很惊讶为什么当我在两者之间使用绑定源时这些行是不可见的。除此之外,现在所有列都有正确的标题(我在 DataTable 中设置),当我使用绑定源时,这些标题会丢失。

【问题讨论】:

  • 您是否尝试设置 autogeneratecolumns = true
  • 我有 devexpress GridView。我只是在这里尝试使用自动生成列替换 Me.UserListGridView.OptionsBehavior.AutoPopulateColumns = True 。仍然是相同的行为。行中没有数据。正在生成行,但数据仍然不可见。
  • 从工具箱中拉出一个winforms datagridview,并尝试为该网格设置数据源并检查它是否加载数据
  • 如果可行,请使用 BindingSource 绑定 devexpress 网格
  • 是的,它正在使用 winforms GridView!我只使用了绑定源。检查我的问题。 GirdView 的数据源是 User_infoDTBindingSource。

标签: c# vb.net winforms gridview devexpress


【解决方案1】:

嗯,我得到了答案。我添加了以下几行:

Me.UserListGridView.Columns.Clear()
Me.UserListGridView.OptionsBehavior.AutoPopulateColumns = True
Me.User_infoDTBindingSource.DataSource = _user_info.User_infoDT
Me.User_infoDTBindingSource.RaiseListChangedEvents = True
Me.User_infoDTBindingSource.ResetBindings(True)

设置数据源后,引发列表更改事件并重置绑定解决了问题。

【讨论】:

    【解决方案2】:

    试试这个

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        DataTable table = new DataTable();
        BindingSource bind = new BindingSource();
    
        public Form1()
        {
          InitializeComponent();
          table.Columns.Add(new DataColumn("Name", typeof(string)));
          table.Columns.Add(new DataColumn("Value", typeof(Int32)));
          bind.DataSource = table;
          dataGridView1.DataSource = bind;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          table.Columns.Add(new DataColumn("Check", typeof(bool)));
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多