简体   繁体   中英

How to get any data from datagridview when I click on the row in datagridview?

How can I get any data from datagridview when I click on the row in datagridview?

I tried this :

String x = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

It always returns a first data in datagridview.

Can anybody help?

You need to set event listener to CellClick event:

dataGridView1.CellClick += DataGridView1_CellClick;

Handler:

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    string s = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM