从Datagridview列填充组合框值 [英] Populate Combobox value from a Datagridview column

查看:91
本文介绍了从Datagridview列填充组合框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经搜索了很多帮助,但是我找到了要在datagridview中获得一个列作为组合框的结果,但是我正在寻找帮助,它将从datagridview列中填充一个comobox.

Hi,

I have searched alot for help but I find results to get a column in datagridview as a combobox but I am looking fro help which will populate a comobox from a datagridview column.

我的表单具有combobox1和1个datagridview(dgv). dgv包含3列,我必须将column(2)值放入comobobox 1列表中.

下面的代码获取值,但是每次在comobobox上单击都会再次添加值.如何只添加一次项目?

My form has combobox1 and 1 datagridview(dgv). dgv contains 3 columns and I have to get the column(2) values into the comobobox 1 list.

The code below gets the values but every click on the comobobox adds the values again. How to get the items added only once?

    Private Sub CmbxCombobox1_MouseClick(sender As Object, e As MouseEventArgs) Handles CmbxCombobox1.MouseClick
        Dim str As String
        Dim Iint As Integer = 0
        For Iint = 0 To DataGridView.Rows.Count - 1
            str = CStr((DataGridView.Rows(Iint).Cells(1).Value))
            CmbxCombobox1.Items.Add(str)
        Next
    End Sub

谢谢
NewBInVB

Thanks
NewBInVB

NewBInVB

推荐答案

您是否使用DataTable作为DGV的数据源?

Do you use a DataTable as the data souce for the DGV?

*

在您的示例中,在其他情况下,最好将CB填充为您执行的方式,每当用户单击列表中的项目时,它都会继续执行

In you example, you might be better to populate the CB in some other event as the way you are doing it, every time the user clicks on an item in the list, it will go ahead and do

整个事情.

*

如果您不想附加旧内容,则需要在重新加载CB之前清除旧内容.

You would need to clear the old contents before reloading the CB if you don't want to append them.

*

如果您想在用户向DGV添加项目时向CB添加项目,则可能 DataGridView CellValidated 事件是一个更好的选择,您可以在其中检查Column是否为2,如果是的话,只需将其添加到CB中(大概是检查它是否已经存在于CB中). CB首先).

If you want to add items to the CB when the user adds items to the DGV, then maybe the DataGridView CellValidated event would be a better choice where you could check if the Column is 2 and if so just add it to the CB (presumably checking it isn't already in the CB first).

*

只是您帖子中的一个混乱点.您在顶部说Column2,然后在代码部分显示Cells(14)!

Just as a point of confusion in your post. You say Column2 in the top part, then show Cells(14) in the code part!


这篇关于从Datagridview列填充组合框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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