vb.net 从数据集中填充数据并显示在 datagridview 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1260170/
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
Populate data from dataset and show in datagridview
提问by Anirudh Goel
I am working in VB.NET and I have a simple requirement:
我在 VB.NET 工作,我有一个简单的要求:
I have added a MDB file to a DataSet and it contains 21 tables.
我已将 MDB 文件添加到 DataSet,它包含 21 个表。
I have a DataGridView and a ComboBox on my form.
我的表单上有一个 DataGridView 和一个 ComboBox。
I was able to get the ComboBox populated with the table names available in the DataSet by iterating through dataset.Tables
.
我能够通过遍历dataset.Tables
.
Now I want to the user to be able to select the table name from the ComboBox and then populate the contents of that table.
现在我希望用户能够从 ComboBox 中选择表名,然后填充该表的内容。
I tried the following code:
我尝试了以下代码:
Datagridview1.DataSource = dataset1
Datagridview1.DataMember = dataset1.tables(combobox1.selecteditem)
Datagridview1.Refresh()
But I only got the column headers. Then I read that I need a TableAdapter to populate the DataSet with that table. But if I use the TableAdapter then I won't be able to populate the table in a generic way.
但我只得到了列标题。然后我读到我需要一个 TableAdapter 来用该表填充 DataSet。但是如果我使用 TableAdapter 那么我将无法以通用方式填充表格。
Currently if I have to populate TableA
then I will have to create an instance of Dataset1TableAdapters.TableA
and then use it's .Fill
property to populate the table. I will also have to use "Dataset1TableAdapters.TableB`. Is there a generic method to populate anytable in the DataSet?
目前,如果我必须填充,TableA
那么我将不得不创建一个实例,Dataset1TableAdapters.TableA
然后使用它的.Fill
属性来填充表。我还必须使用“Dataset1TableAdapters.TableB`。是否有一种通用方法来填充DataSet 中的任何表?
回答by Bliss
The following worked for me:
以下对我有用:
Datagridview1.DataSource = dataset1.tables(combobox1.selecteditem)
回答by Sorin Comanescu
C# but easily convertible to VB.Net (I suppose):
C# 但很容易转换为 VB.Net(我想):
String connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\test.mdb""";
String tableName = combobox1.SelectedItem.ToString();
String sqlSELECT = String.Format("select * from [{0}]", tableName);
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
da.SelectCommand = new System.Data.OleDb.OleDbCommand(sqlSELECT, new System.Data.OleDb.OleDbConnection(connectionString));
DataGridView1.DataSource = dataSet1;
DataGridView1.DataMember = dataSet1.Tables(tableName);
da.Fill(dataSet1, tableName);
I was just sketching a proof that "it can be done in a generic way" to your problem, you should elaborate and maybe come with something more elegant (and safe).
我只是草拟了一个证据,证明“它可以以通用方式完成”解决您的问题,您应该详细说明,并且可能会提供更优雅(和安全)的东西。
回答by renevdkooi
Private Sub BindData()
With customersDataGridView
.AutoGenerateColumns = True
.DataSource = customersDataSet
.DataMember = "Customers"
End With
End Sub
回答by Tad
Just to clarify this answer. For Future Reference.
只是为了澄清这个答案。备查。
Public Class Form1
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\test.accdb;")
Private Sub Button1_Click
Dim da As New OleDbDataAdapter
Dim table As String = ComboBox1.SelectedItem.ToString
Dim sql As String = String.Format("select * from [{0}]", table)
da.SelectCommand = New OleDbCommand(sql, con)
DataGridView1.DataSource = DataSet.Tables(table)
da.Fill(DataSet, table)
End Sub
End Class
Not trying to Ressurect, but i thought the working version of this VB Script should be posted.
不是试图恢复,但我认为应该发布这个 VB 脚本的工作版本。
I would like to point out that the dataset is held by Visual Studio, hence the strange source string.
我想指出数据集由 Visual Studio 保存,因此是奇怪的源字符串。