vini
vini

Reputation: 4732

Index out of range when paging using gridview asp.net

int index = -1;
foreach (GridViewRow gvrow in lstHoliday.Rows)
{
    index = (int)lstHoliday.DataKeys[gvrow.RowIndex].Value;
    bool result = ((CheckBox)gvrow.FindControl("Selector")).Checked;
}

Get an error here:

index = (int)lstHoliday.DataKeys[gvrow.RowIndex].Value;

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

What am I doing wrong here?

enter image description here

Upvotes: 2

Views: 1670

Answers (1)

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

The DataKeyNames property of GridView on the markup needs to be mentioned. It wasnt mentioned above which caused the error.

More on DataKeyNames

Upvotes: 1

Related Questions