out of range error

E

et

I don't understand why this always happens, what am I doing wrong?

I have a grid:
<asp:datagrid id=dg runat="server" AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn Visible="true" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Class Number">
<ItemTemplate>
<%#Container.DataItem("ClassNumber")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="Dropdownlist1"/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


Then I have the ItemDataBound event:
If e.Item.ItemType = ListItemType.EditItem Then
Dim ddl As DropDownList = CType(e.Item.Cells(3).Controls(0),
DropDownList)
ddl.Items.Add("Item1")
ddl.Items.Add("Item2")
...
End If



and it always errs on the cast line:
Dim ddl As DropDownList = CType(e.Item.Cells(3).Controls(0),
DropDownList)

Exception Details: System.ArgumentOutOfRangeException: Specified
argument was out of the range of valid values. Parameter name: index

Even though I know it's cell 3, and control 0, I have tried putting 1, 2, 3,
4, 5 for the cell index and the control index. It doesn't make sense, what
am I missing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top