1.datagridview.CurrentCell 获取当前处于活动状态的单元格。
代码如下。获取当前活动的单元格的行号,列号,值。
CurrentCell
1
public
partial
class
Form1 : Form
2 {
3 private Person person { get ; set ; }
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 }
2 {
3 private Person person { get ; set ; }
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 }