Click to See Complete Forum and Search --> : Grid.DataKeys [stupid?] question


devpro
June 21st, 2001, 05:26 PM
Hi folks, I have the following grid in an ASPX page:

#asp:DataGrid id="Grid1" runat="server"
AllowSorting="True"
AutoGenerateColumns ="False"
DataKeyField="ID"
OnEditCommand="Grid1_Edit"
OnCancelCommand="Grid1_CancelEdit"
OnUpdateCommand="Grid1_Update"
...
...

You can see that DataKeyField is set on "ID". In the code behind file I have
the following code:

protected void Grid1_Update(object sender, DataGridCommandEventArgs e)
{
if (Page.IsValid)
{
int i = (int)Grid1.DataKeys[e.Item.ItemIndex];
...
Grid1.EditItemIndex = -1;
BindGrid();
}
}

If I run the page, I get an error when the
"(int)Grid1.DataKeys[e.Item.ItemIndex];" is processed (cannot dereference a
null...). If I debug, I see that e.Item.ItemIndex has the right value, while
(int)Grid1.DataKeys[e.Item.ItemIndex] says "error: variable out of scope".

Any advice???
Thank you


- devpro