VB.NET - 将列添加到列表中的 DataGridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8730765/
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
VB.NET - Adding columns to DataGridView's in a list
提问by Sultanen
I have a program where i let the user dynamically add DataGridViews to the interface, those are also added to a list for easy management. All of the DataGridViews have their own datasources. I am using a Databinding source to get different DataGridViewTextBoxColumns to add.
我有一个程序,我让用户动态地将 DataGridViews 添加到界面,这些也被添加到列表中以便于管理。所有的 DataGridViews 都有自己的数据源。我正在使用数据绑定源来添加不同的 DataGridViewTextBoxColumns。
The problem is the following: When the user adds the first table the following code runs:
问题如下:当用户添加第一个表时,运行以下代码:
dataGridList(dataGridList.Count -1).Clear()
dataGridList(dataGridList.Count -1).Add(NameDataGridViewTextBoxColumn)
No problem there and the right data is displayed, but when the user adds the second table i get an exception thrown:
那里没问题,并且显示了正确的数据,但是当用户添加第二个表时,我会抛出异常:
"Provided column already belongs to the datagrid view"
“提供的列已经属于数据网格视图”
I think this is strange since i access different DataGridView's right?
我认为这很奇怪,因为我访问了不同的 DataGridView,对吗?
The point of all of this is to be able to view different columns in all the DataGridViews depending on the users choices.
所有这一切的重点是能够根据用户的选择查看所有 DataGridView 中的不同列。
Hope you can help me.
希望您能够帮助我。
回答by Jay
How are you determining what NameDataGridViewTextBoxColumn is? What I mean is how is your list of textBoxColumns you mentioned created?
您如何确定 NameDataGridViewTextBoxColumn 是什么?我的意思是你提到的 textBoxColumns 列表是如何创建的?
It seems likely that you actually need to create a New DataGridViewTextBoxColumn to add with the appropriate properties set.
似乎您实际上需要创建一个新的 DataGridViewTextBoxColumn 来添加适当的属性集。
You can add a column in code something like this:
您可以在代码中添加一列,如下所示:
Dim col As New DataGridViewTextBoxColumn
col.DataPropertyName = "PropertyName"
col.HeaderText = "SomeText"
col.Name = "colWhateverName"
DataGridView1.Columns.Add(col)
Be sure to set the appropriate property name etc. You can copy it from the existing column too...
请务必设置适当的属性名称等。您也可以从现有列中复制它...
col.DataPropertyName = NameDataGridViewTextBoxColumn.DataPropertyName
For example.
例如。
回答by em3ricasforsale
Like above you need a new instance for each grid view and assign the columns to the new gridview which you could do on postback.
像上面一样,您需要为每个网格视图创建一个新实例,并将列分配给您可以在回发时执行的新网格视图。
回答by Aaron Webb
Here is what I did. I choose the column via my DB search or table, then mesh both into one DGV. Like this. I just played with it and it works now to properly align the rows. This is a little off topic as it's DB, but Im guessing similar methods would simplify adding data from other sources.
这是我所做的。我通过我的数据库搜索或表选择列,然后将两者都网格化为一个 DGV。像这样。我刚刚玩过它,现在它可以正确对齐行。这有点离题,因为它是数据库,但我猜测类似的方法会简化从其他来源添加数据的过程。
tempAdt = New OleDbDataAdapter("SELECT firstName FROM Employees",connector)
tableCommand = New OleDbCommandBuilder(tempAdt)
tableAdaptor = New OleDbDataAdapter("SELECT * FROM userTable", connector)
tableCommand = New OleDbCommandBuilder(tableAdaptor)
Dim startTable2 As New DataTable
tempAdt.Fill(startTable2)
tableAdaptor.Fill(startTable2)
Mainly I used two different adapters, and it will list them side by side in DGV
我主要使用了两个不同的适配器,它会在 DGV 中并排列出它们