winform datagridview 获取当前单元格的行号,或者 是列号

本文介绍如何在C#中使用DataGridView控件获取当前活动单元格的行号、列号及值,通过实例演示了如何实现这一功能。

1.datagridview.CurrentCell  获取当前处于活动状态的单元格。

代码如下。获取当前活动的单元格的行号,列号,值。

 

ExpandedBlockStart.gifCurrentCell
 1 public partial class Form1 : Form
 2     {
 3         private Person person { getset; }
 4         public Form1()
 5         {
 6             InitializeComponent();
 7             dataGridView1.DataSource = GetPerson();
 8         }
 9 
10         private BindingList<Person> GetPerson()
11         {
12             BindingList<Person> bl = new BindingList<Person>();
13             for (int i = 0; i < 200; i++)
14             {
15                 bl.Add(new Person("wtq"23"13616009873}"""));
16                 bl.Add(new Person("wtm"23"13616009873}"""));
17                 bl.Add(new Person("wts"23"13616009873}"""));
18                 bl.Add(new Person("wta"23"13616009873}"""));
19                 bl.Add(new Person("wtb"23"13616009873}"""));
20                 bl.Add(new Person("wtc"23"13616009873}"""));
21                 bl.Add(new Person("wtd"23"13616009873}"""));
22                 bl.Add(new Person("wte"23"13616009873}"""));
23                 bl.Add(new Person("wtf"23"13616009873}"""));
24             }
25             return bl;
26         }
27 
28         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
29         {
30             DataGridView dgv = sender as DataGridView;
31             string msg = string.Format("单元格所在的行{0},单元格所在的列{1},单元格所在的值{2}", dgv.CurrentCell.RowIndex, dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.Value);
32             MessageBox.Show(msg);
33             string msg1 = string.Format("单元格所在的行{0},单元格所在的列{1},单元格所在的值{2}", e.RowIndex, e.ColumnIndex,dataGridView1[e.ColumnIndex,e.RowIndex].Value);
34             MessageBox.Show(msg1);
35         }
36 
37 
38     }

 

 

 

 

 

转载于:https://www.cnblogs.com/85538649/archive/2011/09/05/2167076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值