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?

Maybe something like this instead?

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        textBox1.Text = dataGridView1.SelectedCells.ToString();
    }

If I read this method correctly any time someone clicks on a cell in the dataGridView it should populate the textbox with the contents of the selected cell.

Hope this helps :) Please mark as solved if your issue is resolved.

Edit: Unless you're trying to get a specific value within the row no matter which cell in the row is clicked...

In relation to why it's causing an issue when you click the header (using your method) that's because the header row is effectively row -1 of the dataGridView so e.RowIndex would give a value of -1 which doesn't exist within the dataGridView's dataSource.

As a side note this might work for your original code:

if (dataGridView1.Rows[e.RowIndex].Index != -1)
{
    textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
}
else
{
    textBox1.Text = "";
}

Edit: You'd think I could've put all that in one response, but noo... I had to go and TEST stuff between hand lol

Maybe something like this instead?

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        textBox1.Text = dataGridView1.SelectedCells.ToString();
    }

If I read this method correctly any time someone clicks on a cell in the dataGridView it should populate the textbox with the contents of the selected cell.

Hope this helps :) Please mark as solved if your issue is resolved.

Edit: Unless you're trying to get a specific value within the row no matter which cell in the row is clicked...

The value being sent to the textbox is System.Windows.Forms.DataGridViewSelectedCellCollection. It doesn't work

well if my first suggestion doesn't work, what about the 2nd one? :)

well if my first suggestion doesn't work, what about the 2nd one? :)

The same. When clicking the column header, it shows the error.

While I don't specifically have a solution for this I did find this post elsewhere that gives some explanation of why it's happening and one person's description of how they worked around it.

I'll keep looking but I've not really done a lot of dataGridView work in the past.

Sorry :( All out of ideas... all I can find is references to the fact that clicking on the header (triggering column sorts) generates 2 events... one destroying the datagridview content and the other recreating it in the new sort order... Apparently this affects the click event at the same time and causes your error.

Short of creating separate click event handlers to handle the difference between header click and non-header click (which I'm not sure where to start) I don't know if there's a solution to match your needs.

This is only the first step for what I really wanted.

My form1 has a datagridview, which has a lastname and firstname column, and a button named 'EDIT'. When I select a row in the datagrid and then click the button 'EDIT', my form will open which has two textboxes, wherein the selected row from my datagridview will pass the value.

So I can edit the selected row in datagridview from form1 to form2 using the two textbox and update.

How could I do this?

Ok, you just went past the level of knowledge I can comfortably claim about dataGridView at 12:15pm while it's just starting to cool down from exceptionally overly hot out lol.

Gonna stop guessing and looking up scenarios now and hopefully someone more familiar with dataGridView will be able to help.

too bad.. I hope there would be someone to give a solution

I read that one way to fix the problem is to set the visibility of the header to false. But I really need the headers.

I manage to get the values by adding using the following codes:


Form1 Edit Button

Form2 Form2 = new Form2(datagrid.Selected.Row.Cell[0])
Form2.Show();

You think this is fine?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.