Click here to Skip to main content
16,019,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've created a windows form to store and retrieve data from a mysql database. The form displays the data on a dataGridView object (defaultview) and the user can select different entries for further information.
Part of the code follows:
C#
private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            int selection = dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected);            MessageBox.Show(dataGridView1.Rows[selection].Cells[2].Value.ToString());
            manageSelection(selection);
        }

C#
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                int selection = dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected);               MessageBox.Show(dataGridView1.Rows[selection].Cells[2].Value.ToString());
                manageSelection(selection);
            }
        }

C#
void manageSelection(int selection)     
        {
            nameBox.Text = dataGridView1.Rows[selection].Cells[1].Value.ToString();
            addressBox.Text = dataGridView1.Rows[selection].Cells[2].Value.ToString();
            tkBox.Text = dataGridView1.Rows[selection].Cells[11].Value.ToString();
            telBox.Text = dataGridView1.Rows[selection].Cells[3].Value.ToString();
            cellBox.Text = dataGridView1.Rows[selection].Cells[4].Value.ToString();
            tameioBox.Text = dataGridView1.Rows[selection].Cells[5].Value.ToString();
            amBox.Text = dataGridView1.Rows[selection].Cells[6].Value.ToString();
            amkaBox.Text = dataGridView1.Rows[selection].Cells[7].Value.ToString();
            idcardBox.Text = dataGridView1.Rows[selection].Cells[10].Value.ToString();
            cityBox.Text = dataGridView1.Rows[selection].Cells[12].Value.ToString();
        }

The message boxes are used for debugging purposes only. I found out that the message boxes display the correct content but the same instruction when executed inside manageSelection() won't work and I get an exception (out of index). Why is that?
Posted

1 solution

Please put a break point inside the manageSelection method and run the program. When execution stops at the break point, check whether selection value is less than Rows.Count and check whether all the indices given for Cells are valid i.e. less than the Rows[selection].Cells.Count.
Since, out of index error is throw either the Row index is more than the number of rows or the Cell index may be more the number of cells.

[Edit]Code to illustrate usage of CurrentRow method of DataGridView[/Edit]

C#
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    DataGridViewRow currentRow = dataGridView1.CurrentRow;
    if (currentRow != null)
        manageSelection(currentRow);
}

Define manageSelection method as
C#
void manageSelection(DataGridViewRow row){
    nameBox.Text = row.Cells[1].Value.ToString();
    //Similary others are set
}
 
Share this answer
 
v2
Comments
dj dourak 30-Apr-12 7:48am    
I've already done that and the Rows.Count value is indeed less inside the method. The question is why does this happen? Can I avoid it without creating new objects or deleting the method and inserting duplicate code in the appropriate event-handlers?
VJ Reddy 30-Apr-12 8:14am    
The selection is declared as int, but string value is being assigned to it.
If the purpose is to retrieve the values of cells in the current row, the CurrentRow property of DataGridView can be used. Please see the solution, which I have modified accordingly.
dj dourak 30-Apr-12 11:55am    
Thanks for the answer but I don't get it! Selection is declared as int and an integer is assigned to it... (GetFirstRow returns an int)
Setting the rows as parameters would definetly do the job, but I wanted to make the code more readable since there would be about 10 parameters then. I could also create a new object containing all the rows and use it as a parameter for manageSelection.
But the question is still unanswered, why can't I access dataGridView1.Rows... etc inside the method? I have to add that the messagebox displays the correct information which means that the info is there before calling manageSelection()
VJ Reddy 30-Apr-12 12:35pm    
Sorry. I thought the ToString() of the next line as part of the first statement.

Setting row as parameter does not require 10 parameters. Even for the method shown in the question, when selection is passed as parameter, inside the method only one row dataGridView1.Rows[selection] is being used to set the values. Similarly, instead of dataGridView1.Rows[selection], the row parameter of the method will be used for setting the values of all the ten TextBoxes' Text values inside the method. The CurrentRow is mainly provided for the purpose of accessing the current row. Hence, I think it is preferable to use CurrentRow property.
why can't I access dataGridView1.Rows... etc inside the method? is difficult to say unless the whole code is seen and run in the VisualStudio with debugging. Sometimes it is very difficult to find out the reason.
Thank you.
dj dourak 30-Apr-12 13:03pm    
Silly me!
I used the step-into feature of the debugger and finally figured out what was going on...
I have a text-changed event for most of the textboxes, which is used to extract relevant data from the database and copy them to the datagridview control (its contents change as I type on the text-boxes or whenever the text changes inside the boxes for any reason) so I guess that I'll have to circumvent triggering the event before copying the rows...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900