根据datagridview中的另一个单元格获取单元格的值 [英] Get value of cell based on another cell from datagridview

查看:97
本文介绍了根据datagridview中的另一个单元格获取单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,其中有两列'Name'和'ID',如下所示。



**名称 - ID **

John - 2454

Sammy - 2343

Sarah - 3456



现在,我希望当我从名称列中选择任何名称时,我应该得到名称对应的ID。例如,如果我点击'John'我应该在变量中得到'2454'。



那么它在C#中的可能性如何。??

注意:我的数据视图是从Arraylist填充的。

i have a datagridview in which it has 2 columns 'Name' and 'ID' like below.

**Name--ID**
John--2454
Sammy--2343
Sarah--3456

Now , i want that when i select any name from 'Name' column i should get name corresponding ID. for example if i click on 'John' i should get '2454' in variable.

So how its Possible in C#.??
Note: My datagriview is populated from Arraylist.

推荐答案

使用 DataGridView.CellClick事件 [ ^ ]并使用 dataGridView1.Rows获取行[e.RowIndex] 并且您还可以使用单元格索引获取同一行中的其他单元格值,例如
use DataGridView.CellClick Event[^] and get the row using dataGridView1.Rows[e.RowIndex] and also you can get other cell values in the same row using cell index, for example
dataGridView1.Rows[e.RowIndex].Cells[1]


你好,

首先使用DatGridview.CellClick事件那个已经由DamithSL描述,然后通过这种方式检查单击单元格的ColumnIndex。

Hello ,
First use DatGridview.CellClick Event that already described by DamithSL and then check the ColumnIndex of the clicked cell by this way .
var columnindex= dataGridView.CurrentCell.ColumnIndex;
//Now add 1 for get next  column index
var nextcolumnindex=columnindex+1 ;

//and ultimately get the value of next column by this way
var nextcolumnvalue =dataGridView.Rows[e.RowIndex].Cells[nextcolumnindex].Value;







谢谢




thanks


这篇关于根据datagridview中的另一个单元格获取单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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