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

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

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

 

CurrentCell
 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     }

 

 

 

 

 

posted on 2011-09-05 10:21  wtq  阅读(8253)  评论(0编辑  收藏  举报