Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView get row values

I am trying to get the cell values of the row that I clicked.

here's my code..

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        txtFullName.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
        txtUsername.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
        txtPassword.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
    }

works fine.. but it doesn't work when I click on the row(left side of the UserID) and on user id column... it also gives me an error when I click on the column header. how can I fix that error and I want it to also click on row and userid column.

enter image description here

like image 800
Sam Teng Wong Avatar asked Aug 31 '26 06:08

Sam Teng Wong


1 Answers

You are using the wrong event: Try this

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex > -1)
        {
            var val = this.dataGridView1[e.ColumnIndex,  e.RowIndex].Value.ToString();
        }
    }
like image 108
Gregg Avatar answered Sep 01 '26 19:09

Gregg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!