System.ArgumentOutOfRangeException: Index was out of range in datagrid

I have a filled datagrid and i want to pull the product id's which is the first column

Dim dgi As DataGridItem

For Each dgi In DataGrid1.Items

Dim productId As Integer = Convert.ToInt32(DataGrid1.DataKeys(dgi.ItemIndex))

'do stuff

Next

but i get the error

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

Source Error:

Line 252:    For Each dgi In DataGrid1.Items
Line 253:
Line 254:      Dim productId As Integer = Convert.ToInt32(DataGrid1.DataKeys(dgi.ItemIndex))

Stack Trace:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
  System.Collections.ArrayList.get_Item(Int32 index) +85
  System.Web.UI.WebControls.DataKeyCollection.get_Item(Int32 index)
  prosite.WebForm1.Button5_Click(Object sender, EventArgs e) in C:\Inetpub\testsite\prosite\WebForm1.aspx.vb:254
  System.Web.UI.WebControls.Button.OnClick(EventArgs e)
  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  System.Web.UI.Page.ProcessRequestMain()

i saw elsewhere other errors like this and it was a bug and i applied .net 1.1sp1 and i still get this, any ideas -cmh




Answer this question

System.ArgumentOutOfRangeException: Index was out of range in datagrid

  • Misswing

    Hi cmh,

    It looks like you are using the Web Forms DataGrid control, rather than the Windows Forms DataGrid. This is a Windows Forms forum, so you might want to try your question at the Data Presentation Controls forum at asp.net.



  • System.ArgumentOutOfRangeException: Index was out of range in datagrid