WHEN I RUN AND CLICK ON IN ONE ROW THEN THERE IS AND ERROR ON IT HO TO SOLVE THIS AND ERROR IN EMAIL

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            guest_id.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            first_name.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            last_name.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            txt_gender_status.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            txt_martial_status.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            phone_numb.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
            email_id.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();

        }

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Recommended Answers

All 19 Replies

SelectedRows won't necessarily be populated from a cell event. You could use SelectedCells[i].OwningRow to get your "selected" row and attached cells:

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    var row = dataGridView1.SelectedCells[0].OwningRow;

    guest_id.Text = row.Cells[0].Value.ToString();
    first_name.Text = row.Cells[1].Value.ToString();
    last_name.Text = row.Cells[2].Value.ToString();
    txt_gender_status.Text = row.Cells[3].Value.ToString();
    txt_martial_status.Text = row.Cells[4].Value.ToString();
    phone_numb.Text = row.Cells[5].Value.ToString();
    email_id.Text = row.Cells[6].Value.ToString();
}

But I mention that only because it's handy information. In the context of a cell mouse event, the event args give you the row index, which is a vastly superior method:

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    var row = dataGridView1.Rows[e.RowIndex];

    guest_id.Text = row.Cells[0].Value.ToString();
    first_name.Text = row.Cells[1].Value.ToString();
    last_name.Text = row.Cells[2].Value.ToString();
    txt_gender_status.Text = row.Cells[3].Value.ToString();
    txt_martial_status.Text = row.Cells[4].Value.ToString();
    phone_numb.Text = row.Cells[5].Value.ToString();
    email_id.Text = row.Cells[6].Value.ToString();
}
  var row = dataGridView1.Rows[e.RowIndex];

when i add this line again given that errors

sigh

The var keyword was introduced 5 years ago. Unless you're specifically maintaining legacy code, upgrade your tools! But since I know you won't, this will fix that error:

DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

alwyz not solve

commented: You make helping you impossible -2

alwyz not solve

Post the error.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
same error is gven

Do you have any rows in the datagridview? Hop into the debugger and check your indexes as well as the size of each collection being indexed.

yeah i have rows in gridview when i drag the mouse in any row and click on it then all data coming in textboxes but the email id not be shown and the error is given which i mentioned...

and how i check indexes and size

and how i check indexes and size

Do you know how to run the debugger?

yes

yes i know how to debug

Which line gives the error?

LAST LINE :/

So there's something up with Cell[6]. Does your datagridview have 7 columns? Hop into the debugger and check the Cells collection, make sure that index 6 exists and has viable data in it.

yeah i have 7 columns like as
0 1 2 3 4 5 6 (cells number)
1 2 3 4 5 6 7(text boxes)

and i hve also check cells and index has variable in it

This is like pulling teeth. Does anything look funny when you look over the elements of that line in the debugger? The exception isn't random just to wake you up, it's caused by something not right. And since the exception mentions an index out of range, the problem is an index out of range or a null where one is assumed to never be present.

i dont understand ur point......
nd what is the reason behind that index is out of range

i dont understand ur point......

My point is that something is wrong, you know what line it's on, and I can't look over your shoulder to properly hold your hand while debugging.

nd what is the reason behind that index is out of range

That's the question you're trying to answer by debugging the exception. You've rejected all of the possibilities I've pointed out, so now it's up to you to debug the line, add breakpoints, add watches, hover over shit, and look for values that aren't what you expected.

Thankuuuuuuuu SO much i solve error

in store procedure i dont mention the guestid

i correct inner join query and solve

thanks

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.