This is a migrated thread and some comments may be shown as answers.

CellClick in RadGridView doesn't work. why ?

3 Answers 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kuswandi
Top achievements
Rank 1
Kuswandi asked on 25 Dec 2010, 05:58 AM
sorry, I can not speak english very well.

why this error?

this my code :

private void gridNegara_CellClick(object sender, GridViewCellEventArgs e)
{
            int i = gridNegara.CurrentRow.Index;
            txtKodeNeg.Text = (gridNegara.Rows[i].Cells[0].Value).ToString();
            txtNamaNeg.Text = (gridNegara.Rows[i].Cells[1].Value).ToString();
            txtIbuKotaNeg.Text = (gridNegara.Rows[i].Cells[2].Value).ToString();
}

gridNegara = RadGridView
txtKodeNeg, txtNamaNeg, txtIbuKotaNeg = RadTextBox

when running visual studio (F5), an error message appears:

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


It happens when I click on FilteringRow

thanks for your attention.

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 25 Dec 2010, 06:28 PM
Hello,

The problem here is that you are using the index to get the value of the cells and the index of the filtering row and the new row is -1 (for both).

If you want just to get the values you can just use the CurrentRow.Cells, like so:
private void gridNegara_CellClick(object sender, GridViewCellEventArgs e)
{
    if  (gridNegara.CurrentRow == null)
    {
           return;
    }
 
    txtKodeNeg.Text = (gridNegara.CurrentRow.Cells[0].Value).ToString();
    txtNamaNeg.Text = (gridNegara.CurrentRow.Cells[1].Value).ToString();
    txtIbuKotaNeg.Text = (gridNegara.CurrentRow.Cells[2].Value).ToString();
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Kuswandi
Top achievements
Rank 1
answered on 26 Dec 2010, 10:39 AM
Hi Emanuel Varga,

thank you for your answer

I have tried your code, the result is error. error message:
"Object reference not set to an instance of an object".

but, I try with this code and successfully:

int i = gridNegara.CurrentRow.Index;
if (i < 0)
{
     
}
else
{
    txtKodeNeg.Text = (gridNegara.Rows[i].Cells[0].Value).ToString();
    ... etc
}

thank you
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Dec 2010, 08:04 PM
Hello again,

It is throwing a null reference because either you are not checking for null values before calling a ToString().
Please check the .Value before calling ToString and it will work.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
Tags
GridView
Asked by
Kuswandi
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Kuswandi
Top achievements
Rank 1
Share this question
or