I used this.
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
}
It works, but when I click the column header of the gridview, it shows an error.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:index
How to fix this?