OnItemCreated?

A

Arpan

Consider the following code which populates a DataGrid with the items
of a string array:

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If (ViewState("Colors") Is Nothing) Then
arrColors = New String(3) {"red", "blue", "pink", "white"}
ViewState("Colors") = arrColors
Else
arrColors = ViewState("Colors")
End If

'creating a DataTable with 2 columns - ID & Color,
'adding the DataTable to a DataSet, adding rows
'to the DataTable & finally populating the DataSet

dgColors.DataBind()
End Sub

Sub ChangeColor(ByVal obj As Object, ByVal ea As DataGridItemEventArgs)
Dim intIndex As Integer = ea.Item.ItemIndex
Response.Write("Index: " & intIndex & "<br>")
End Sub

<form runat="server">
<asp:DataGrid ID="dgColors" OnItemCreated="ChangeColor" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"><%# Container.DataItem("ID")
%></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Color" HeaderText="COLOR"/>
<asp:EditCommandColumn HeaderText="CHANGE" EditText="EDIT"
UpdateText="CHANGE" CancelText="CANCEL"/>
</Columns>
</asp:DataGrid>
</form>

Note the Response.Write line in the sub "ChangeColor". That line
produces the following output:

Index: -1
Index: 0
Index: 1
Index: 2
Index: 3
Index: -1

Now where from is the value "-1" coming, that too, not once but twice?

Thanks,

Arpan
 
G

Guest

Arpan,

That should be for the header and footer rows of the data grid.

Regards,
Augustin
 

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

Similar Threads

DropDownList DataGrid 1
DataGrid Edit Problem 9
Controls 2
Controls? 15
Cells.Controls 0
Dynamic BoundColumn 2
Difference 2
Problem with Data Grid 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top