Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

index out of range error

Status
Not open for further replies.

slwolf8

Technical User
Apr 23, 2001
82
US
I am attempting to write some code to delete a row from a datagrid. My code is throwing the index out of range error right on the first line of code in my deletecommand event:

Dim strSubCatID As Int32 = dgSubCat.DataKeys(e.Item.ItemIndex)

Dim dr As DataRow
Dim i As Integer
For i = 0 To DAcmp.DsBudget.Tables("spSelSubCat").Rows.Count - 1
dr = DAcmp.DsBudget.Tables("spSubCat").Rows(i)
If dr(0) = strSubCatID Then
Exit For
End If
Next
dr.Delete()
DAcmp.daSubCat.Update(DAcmp.DsBudget, "spSelSubCat")
SetDS()


Any ideas?
 
Is your DataKeyField set for the Datagrid?

<asp:datagrid id="DataGrid1" runat="server" Width="700px" OnSortCommand="DataGrid1_SortCommand" AllowSorting="True" BorderStyle="None" BorderWidth="1px" GridLines="Vertical" BorderColor="#999999" BackColor="White" CellPadding="3" AutoGenerateColumns="False" OnDeleteCommand="MyDataGrid_DeleteCommand" DataKeyField="ID">

I had trouble with this once and I was missing the DataKeyField.

DH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top