问题
Like the title says, I'm trying to populate a combo box column in a datagridview.
Here's what i have so far:
Dim lc As System.Web.UI.WebControls.ListItemCollection = _
DataAccess.Part.GetListItems()
dgvcboPart.DataSource = lc
' This is a standalone combo box and it works ok
cboTest.DataSource = lc
Any suggestions as to what I'm missing ?
Thanks Tony W
回答1:
I first suggest you bind your Collection to a BindingSource and then add the BindingSource to the DataGridView (so you know the position)
But binding a ComboBoxCell should be pretty much straight forward.
Let's say you have a DataTable tblCurrency containing two columns Id and Name. You have to bind this to your Column (I assume Column 0 ist your DataGridViewColumn)
dgvcboPart.Columns(0).DataSource = tblCurrency
dgvcboPart.Columns(0).ValueMember = "Id"
dgvcboPart.Columns(0).DisplayMember = "Name"
Then you can set the DataPropertyName to the Property in your DataSource.
dgvcboPart.Columns(0).DataPropertyName = "Currency_Id"
Be carful, tblCurrency.Id and Currency_Id have to be of the same Type (Int32 and UInt32 does not work) And you get a nasty MessageBox with a full StackTrace if Currency_Id has a value that is not in tblCurrency (so you should handle the DataError event)
回答2:
dgvcboPart.DataSource = lc
dgvcboPart.DataBind()
have to call the databind method for the magic to happen!
来源:https://stackoverflow.com/questions/865141/populating-a-datagridview-combobox-column-with-subsonic-vb-net