winform datagridview 擷取目前的儲存格的行號,或者 是列號

來源:互聯網
上載者:User

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     }

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.