我有一个包含三列的datagridview,使用Datareader填充. datagridView中有一个DatagridViewComboboxcolumn. 我希望这个DatagridViewComboboxcolumn也应该使用datareader填充. 请建议如何使用Datareader将项添加到Data
我希望这个DatagridViewComboboxcolumn也应该使用datareader填充.
请建议如何使用Datareader将项添加到DatagridViewComboboxcolumn.
以下是我尝试过的代码.
这里的博士是SqlDatareader
Datagridview.Rows.Add(dr("Column1").ToString, dr("Column2"),dr("DatagridViewComboboxcolumn "))
但是,当我添加这种方式我在DatagridViewComboboxcolumn列上获取错误.
请建议
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)