VB.NET中,运行时如何在DataGridView的ComboBoxColumn动态添加选项项?

2026-05-06 12:451阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计373个文字,预计阅读时间需要2分钟。

VB.NET中,运行时如何在DataGridView的ComboBoxColumn动态添加选项项?

我有一个包含三列的DataGridView,使用Datareader填充。DataGridView中有一列是DataGridViewComboBoxColumn。我希望这个DataGridViewComboBoxColumn也使用datareader填充。请建议如何使用Datareader将项添加到Data中。

我有一个包含三列的datagridview,使用Datareader填充. datagridView中有一个DatagridViewComboboxcolumn.

我希望这个DatagridViewComboboxcolumn也应该使用datareader填充.

请建议如何使用Datareader将项添加到DatagridViewComboboxcolumn.
以下是我尝试过的代码.

这里的博士是SqlDatareader

Datagridview.Rows.Add(dr("Column1").ToString, dr("Column2"),dr("DatagridViewComboboxcolumn "))

但是,当我添加这种方式我在DatagridViewComboboxcolumn列上获取错误.
请建议

如前所述,您无法将DataGridViewColumn的DataSource设置为DataReader(因为这是一个仅向前的数据库连接对象).但是你可以做的是填充DataTable并将DataGridViewColumn上的DataSource设置为此DataTable.您还需要设置DataPropertyName(这是DataGridView的数据源的列名),ValueMemeber和DisplayMember.下面的示例使用Adventureworks DB并使用4列填充DataGridView(其中一列是组合框 – ProductIdCombo).只需创建一个表单,在其上放一个名为DataGridView1的DataGridGridView控件并运行以下命令. ProductId列演示绑定到组合(ProductIdCombo列)的基础列确实更新了dtProductsInventory DataTable中的ProductId字段.

Dim dtProductInventory As New System.Data.DataTable Dim dtProducts As New System.Data.DataTable Using objSqlServer As New System.Data.SqlClient.SqlConnection("Server=LOCALHOST\SQLEXPRESS; Integrated Security=SSPI;Initial Catalog=AdventureWorks") objSqlServer.Open() Dim sqlCmd As New System.Data.SqlClient.SqlCommand("select * from production.ProductInventory", objSqlServer) dtProductInventory.Load(sqlCmd.ExecuteReader) sqlCmd.CommandText = "Select * from production.product" dtProducts.Load(sqlCmd.ExecuteReader) End Using DataGridView1.AutoGenerateColumns = False DataGridView1.DataSource = dtProductInventory Dim colProductIdCombo As New System.Windows.Forms.DataGridViewComboBoxColumn() colProductIdCombo.DataSource = dtProducts colProductIdCombo.DisplayMember = "Name" colProductIdCombo.ValueMember = "ProductId" colProductIdCombo.DataPropertyName = "ProductId" colProductIdCombo.HeaderText = "ProductIdCombo" DataGridView1.Columns.Add(colProductIdCombo) Dim colProductId As New System.Windows.Forms.DataGridViewTextBoxColumn() colProductId.DataPropertyName = "ProductId" colProductId.HeaderText = "ProductId" DataGridView1.Columns.Add(colProductId) Dim colShelf As New System.Windows.Forms.DataGridViewTextBoxColumn() colShelf.DataPropertyName = "Shelf" colShelf.HeaderText = "Shelf" DataGridView1.Columns.Add(colShelf) Dim colQuantity As New System.Windows.Forms.DataGridViewTextBoxColumn() colQuantity.DataPropertyName = "Quantity" colQuantity.HeaderText = "Quantity" DataGridView1.Columns.Add(colQuantity)

VB.NET中,运行时如何在DataGridView的ComboBoxColumn动态添加选项项?

本文共计373个文字,预计阅读时间需要2分钟。

VB.NET中,运行时如何在DataGridView的ComboBoxColumn动态添加选项项?

我有一个包含三列的DataGridView,使用Datareader填充。DataGridView中有一列是DataGridViewComboBoxColumn。我希望这个DataGridViewComboBoxColumn也使用datareader填充。请建议如何使用Datareader将项添加到Data中。

我有一个包含三列的datagridview,使用Datareader填充. datagridView中有一个DatagridViewComboboxcolumn.

我希望这个DatagridViewComboboxcolumn也应该使用datareader填充.

请建议如何使用Datareader将项添加到DatagridViewComboboxcolumn.
以下是我尝试过的代码.

这里的博士是SqlDatareader

Datagridview.Rows.Add(dr("Column1").ToString, dr("Column2"),dr("DatagridViewComboboxcolumn "))

但是,当我添加这种方式我在DatagridViewComboboxcolumn列上获取错误.
请建议

如前所述,您无法将DataGridViewColumn的DataSource设置为DataReader(因为这是一个仅向前的数据库连接对象).但是你可以做的是填充DataTable并将DataGridViewColumn上的DataSource设置为此DataTable.您还需要设置DataPropertyName(这是DataGridView的数据源的列名),ValueMemeber和DisplayMember.下面的示例使用Adventureworks DB并使用4列填充DataGridView(其中一列是组合框 – ProductIdCombo).只需创建一个表单,在其上放一个名为DataGridView1的DataGridGridView控件并运行以下命令. ProductId列演示绑定到组合(ProductIdCombo列)的基础列确实更新了dtProductsInventory DataTable中的ProductId字段.

Dim dtProductInventory As New System.Data.DataTable Dim dtProducts As New System.Data.DataTable Using objSqlServer As New System.Data.SqlClient.SqlConnection("Server=LOCALHOST\SQLEXPRESS; Integrated Security=SSPI;Initial Catalog=AdventureWorks") objSqlServer.Open() Dim sqlCmd As New System.Data.SqlClient.SqlCommand("select * from production.ProductInventory", objSqlServer) dtProductInventory.Load(sqlCmd.ExecuteReader) sqlCmd.CommandText = "Select * from production.product" dtProducts.Load(sqlCmd.ExecuteReader) End Using DataGridView1.AutoGenerateColumns = False DataGridView1.DataSource = dtProductInventory Dim colProductIdCombo As New System.Windows.Forms.DataGridViewComboBoxColumn() colProductIdCombo.DataSource = dtProducts colProductIdCombo.DisplayMember = "Name" colProductIdCombo.ValueMember = "ProductId" colProductIdCombo.DataPropertyName = "ProductId" colProductIdCombo.HeaderText = "ProductIdCombo" DataGridView1.Columns.Add(colProductIdCombo) Dim colProductId As New System.Windows.Forms.DataGridViewTextBoxColumn() colProductId.DataPropertyName = "ProductId" colProductId.HeaderText = "ProductId" DataGridView1.Columns.Add(colProductId) Dim colShelf As New System.Windows.Forms.DataGridViewTextBoxColumn() colShelf.DataPropertyName = "Shelf" colShelf.HeaderText = "Shelf" DataGridView1.Columns.Add(colShelf) Dim colQuantity As New System.Windows.Forms.DataGridViewTextBoxColumn() colQuantity.DataPropertyName = "Quantity" colQuantity.HeaderText = "Quantity" DataGridView1.Columns.Add(colQuantity)

VB.NET中,运行时如何在DataGridView的ComboBoxColumn动态添加选项项?