populating a datagridview combobox column with sub

2019-08-16 01:30发布

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

2条回答
等我变得足够好
2楼-- · 2019-08-16 01:53
dgvcboPart.DataSource = lc
dgvcboPart.DataBind()

have to call the databind method for the magic to happen!

查看更多
\"骚年 ilove
3楼-- · 2019-08-16 02:01

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)

查看更多
登录 后发表回答