我有一个包含3列DataGridView(metric_key,metric_name,metric_value)。
I have a DataGridView with 3 columns (metric_key, metric_name, metric_value).
例如,这个表中有6个ROWS(6个不同的指标),以编程方式增加(DGV不以任何方式约束)
There are for example, 6 ROWS in this table (6 different metrics), added programmatically (DGV is not bound in any way)
现在,当我分配值我的网格我做:
Now, when I assign values in my grid I do:
dataGridView1.item("Metric_value",0).value = "value of the metric in row 0, in the column named "metric_Value".我想要什么
然而,我正在寻找一种方法来使用 metric_Key 列中的值来执行我的任务,例如:
What I'd like
However I'm looking for a way to use the value in the metric_Key column to do my assignment, something like:
datagridView1.item("metric_Value","My_metric_Key")而不是放置行ID,或者可能:
instead of putting the row ID, or maybe:
datagridview1.item("metric_value",Indexof("My_metric_Key"))或
datagridview1.item("metric_value",findValue("my_metric_key","metric_key"))等
我不想创建任何其他对象(dataview等)。
I don't want to create any other objects (dataview, etc.).
我该怎么做?
推荐答案没有内置的方法来做到这一点。但是您可以在DataGridView上定义一个扩展方法来实现此目的。
There isn't a built in way to do this. But you can define an extension method on DataGridView to accomplish this.
<System.Runtime.CompilerServices.Extension()> _ Function FindValue(ByRef dgv As DataGridView, ByVal metric_key As Object) As DataGridViewRow For Each row As DataGridViewRow In dgv.Rows If row.Cells.Item("metric_value").Value = metric_key Then Return row End If Next Return Nothing End Function所以如果你想获得metric_value为1的行,你可以使用
So if you wanted to get the row where the metric_value is 1, you could use
dataGridView1.FindValue(1)更多推荐
以编程方式选择一个基于gridview值的行
发布评论