用亚音速和; 填充 datagridview 组合框列 VB.Net

发布于 2024-07-19 20:50:05 字数 334 浏览 9 评论 0原文

就像标题所说,我正在尝试填充 datagridview 中的组合框列。

到目前为止,这是我所拥有的:

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

对于我缺少的内容有什么建议吗?

谢谢 托尼·W

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够爱げ你 2024-07-26 20:50:06

我首先建议您将 Collection 绑定到 BindingSource,然后将 BindingSource 添加到 DataGridView (这样您就知道位置),

但是绑定 ComboBoxCell 应该非常简单。

假设您有一个包含 Id 和 Name 两列的 DataTable tblCurrency。
您必须将其绑定到您的列(我假设第 0 列是您的 DataGridViewColumn)

     dgvcboPart.Columns(0).DataSource = tblCurrency
     dgvcboPart.Columns(0).ValueMember = "Id"
     dgvcboPart.Columns(0).DisplayMember = "Name"

然后您可以将 DataPropertyName 设置为数据源中的属性。

     dgvcboPart.Columns(0).DataPropertyName = "Currency_Id"

小心,tblCurrency.Id和Currency_Id必须是相同的类型(Int32和UInt32不起作用),如果Currency_Id有一个不在tblCurrency中的值,你会得到一个带有完整StackTrace的令人讨厌的MessageBox(所以你应该处理DataError)事件)

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)

羁客 2024-07-26 20:50:06
dgvcboPart.DataSource = lc
dgvcboPart.DataBind()

必须调用数据绑定方法才能发生奇迹!

dgvcboPart.DataSource = lc
dgvcboPart.DataBind()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文