vb.net 将 GridView 绑定到动态 DataTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26583727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:24:28  来源:igfitidea点击:

Binding GridView to dynamic DataTable

c#vb.netwinformsgridviewdevexpress

提问by IFlyHigh

I have a GridView and I am binding it to a DataTable which is being filled at run time. Number of columns and rows are not fixed. After the DataTable is filled I am setting the binding source of the GridView to this data table:

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

 Me.User_infoDTBindingSource.DataSource = User_infoDT

Now the prblem is, gird view is not showing any columns or rows at all. I used :

现在问题是,网格视图根本不显示任何列或行。我用了 :

Me.UserListGridView.PopulateColumns(User_infoDT)

Now I am getting all the columns in the GridView but the column captions which I setted in DataTable are all lost. The rows ar still empty in the GridView. I have also used OptionView.EnableAppearanceEvenRowso I can see that number of rows are generating in my GridView (as it is supposed to be) but still I can't see any data.

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

I have implemented the same code with Windows DataGridView and TreeList using the same data source (data table) and it is working perfectly. But here I can not find any reason of not getting the rows.

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

Any idea where are these rows? I debugged the code and checked the DataTable when I am setting the binding source's data source to data table. At that time GridView.DataRowCount and GridView.Columns.Count are correct. But still grid view is not showing any data in the rows.

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

UPDATE: I have now removed the binding source and setted the GridView's datasource directly to the DataTable and it is working perfectly!! I am amazed why the rows are invisble when I am using a binding source in between. Apart from this, here now all the columns have correct captions also (which I setted in DataTable) which are being lost when I am using binding source.

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

回答by Alexander Mikhailove

Try this

尝试这个

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)));
    }
  }
}

回答by IFlyHigh

Well I got the answer. I added following lines:

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

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)

After setting the datasource, raising the list changed events and resetting binding resolved the issue.

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