以编程方式选择一个基于gridview值的行

编程入门 行业动态 更新时间:2024-10-19 20:24:11
本文介绍了以编程方式选择一个基于gridview值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含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值的行

本文发布于:2023-11-24 20:57:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!