populating a datagridview combobox column with subsonic & vb.net

Deadly 提交于 2019-12-24 22:50:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!