如何以编程方式在VB.net中填充DataGridViewComboBoxColumn? [英] How to programatically populate a DataGridViewComboBoxColumn in VB.net?

查看:244
本文介绍了如何以编程方式在VB.net中填充DataGridViewComboBoxColumn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此我已经挠了一段时间。

I have been scratching my head for a while over this.

所以我在设计模式下向表单添加了 datagridview

So I have added in design mode a datagridview to my form.

datagridview有两列,一列是文本框列,另一列是组合框列。

The datagridview has 2 columns, one column is textbox column, the other column is combobox column.

我已经找到了如何以编程方式填充文本框的单元格,但是我无法确定要使用哪个属性来填充组合框列。

I have figured out how to programmatically populate the cells of a textbox, however I can't figure out which property to use to populate the combobox column.

我只是想在下拉列表中添加3个选项。任何想法都很棒。

I am simply looking to have a dropdown with 3 options. Any ideas would be great.

PS:我两天前才刚接过VB.net,所以我很抱歉这个问题是否很原始:)

P.S: I just picked up VB.net 2 days ago so I apologize if the question is primitive :)

推荐答案

如果组合框中有 DataSource ,则可以这样做

If you have a DataSource in your combobox, you can do this

Dim dgvcc As New DataGridViewComboBoxCell
With dgvcc
   .DataSource = answerStr
   .ValueMember = "CampaignAnswerId"
   .DisplayMember = "Answer"
End With

DataGridViewName.Item(columnIndex, rowIndex) = dgvcc

或者您可以这样做

Dim dgvcc As New DataGridViewComboBoxCell
dgvcc.Items.Add("test1")
dgvcc.Items.Add("test2")
dgvcc.Items.Add("test3")

DataGridViewName.Item(columnIndex, rowIndex) = dgvcc

请注意,在<$ c内循环时必须执行此操作$ c> DataGridView 。

For rowIndex as integer = 0 To DataGridViewName.Rows.Count - 1
    Dim dgvcc As New DataGridViewComboBoxCell
    dgvcc.Items.Add("test1")
    dgvcc.Items.Add("test2")
    dgvcc.Items.Add("test3")

    DataGridViewName.Item(yourtextboxcolumnIndex, rowIndex) = dgvcc
Next

这篇关于如何以编程方式在VB.net中填充DataGridViewComboBoxColumn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆