从DataGridView ComboBoxColumn获取价值 [英] Get value from DataGridView ComboBoxColumn

查看:83
本文介绍了从DataGridView ComboBoxColumn获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我刚刚创建了一个带有DGV的表单,并将其中一列设置为DataGridViewComboBoxColumn.将数据源绑定到它,并在设计器中全部设置正确的属性.

I just created a form with a DGV on it and set one of the columns to DataGridViewComboBoxColumn. Bound a DataSource to it, and set the right properties, all in the Designer.

列名:datagridviewtextboxcolumn4

Column Name : datagridviewtextboxcolumn4

值成员为"Id"

DisplayMember为说明"

DisplayMember is 'Description'

如果我正在运行该应用程序,则会看到我的列,并且在DataGridViewComboBoxColumn中会看到与ID相关的描述(值).没问题.

If I'm running the app, I see my columns, and in the DataGridViewComboBoxColumn I see the Description (value) related to the Id. No problem.

但是我需要将该描述的存储在变量中.

But I need to store  the value of that description in a variable.

我希望有这样的事情:

 Dim variable = DataGridViewTextBoxColumn4.DisplayMember.value

但是没有

如果我这样做

Dim variable = DataGridViewTextBoxColumn4.DisplayMember

变量get是有意义的值"Description",因为我要这样做.

variable get's the value 'Description' that's makes sense, because I'm asking to do so.

我找到了很多有关组合框的代码,但是在以下位置找不到有关ComoboxColumn 的代码 DGV.

I've found a lot of code about comboboxes, but I can't find some code for a ComoboxColumn in a DGV.

是否可以将Description的值直接获取到变量中?

Is it possible to get the value of the Description directly into a variable?

致谢

标记

推荐答案

你好,

以下 VS2015中完成的代码示例展示了如何

The following code sample done in VS2015 shows how to 

  • 设置一个DataGridViewComboBoxColumn来保存显示值和一个值成员,但不通过设计器,而是通过代码.
  • 获取该列特定行的值.

代码的详细信息

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim operation As New DataOperations 
    operation.GetData() 
    If Not operation.Exception.HasError Then 
        ColorDictionary = operation.ColorDictionary 
 
        DataGridView1.AutoGenerateColumns = False 
        bsComboBox.DataSource = operation.ColorTable 
 
        ColorsColumn.DisplayMember = "ColorText" 
        ColorsColumn.ValueMember = "ColorId" 
        ColorsColumn.DataSource = bsComboBox 
        ColorsColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing 
 
        bsPerson.DataSource = operation.PersonsTable 
        DataGridView1.DataSource = bsPerson 
 
        bsColorInformation.DataSource = operation.InformationTable 
 
        updateFormControls() 
 
    End If 
 
End Sub

在设计器中

要获得价值(请参阅我的 代码示例),我们需要订阅DataGridView的EditingControlShowing事件,该事件在DataGridViewComboBox的单元格下方为ComboBox设置了一个事件.在这里,我们可以获取价值.

To get at the value (refer to my code sample) we need to subscribe to EditingControlShowing event of the DataGridView which sets up an event for the ComboBox beneath the cell for the DataGridViewComboBox. Here we can get to the value.

这里原始值是蓝色,我们选择绿色,然后得到值,首先蓝色的值是2,选择绿色,我们得到3.

Here the original value is blue, we select green and get the value, first the value for blue is 2, selecting green we get 3.

数据库中的数据

我本来可以选择性别"或其他任何字段,但它们的工作原理相同.

I could had selected Gender instead or any other field, it all works the same.


这篇关于从DataGridView ComboBoxColumn获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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