This was the original line in code-behind (CType(row.Cells(0).Controls(0), TextBox)) and it didn't work. After that, I changed it to row.Cells(1) and received the above error when attempting to update a field in GridView. Here're the aspx markups:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="TicketID" DataSourceID="historySqlDataSource" ForeColor="#333333"
GridLines="None" Width="828px" OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="TicketID" HeaderText="TicketID"
InsertVisible="False" ReadOnly="True" SortExpression="TicketID" />
<asp:BoundField DataField="DateCreated" HeaderText="DateCreated"
SortExpression="DateCreated" />
<asp:BoundField DataField="FullName" HeaderText="FullName"
SortExpression="FullName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="TicketType" HeaderText="TicketType"
SortExpression="TicketType" />
<asp:BoundField DataField="Subject" HeaderText="Subject"
SortExpression="Subject" />
<asp:BoundField DataField="Message" HeaderText="Message"
SortExpression="Message" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
And below are the code-behind:
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
BindData()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = 1
BindData()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim dt = CType(Session("dt"), DataTable)
'if your current DataSource be in Session
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
dt.Rows(row.DataItemIndex)("TicketID") = (CType(row.Cells(1).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("DateCreated") = (CType(row.Cells(2).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("FullName") = (CType(row.Cells(3).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("TicketType") = (CType(row.Cells(4).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("Subject") = (CType(row.Cells(5).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("Message") = (CType(row.Cells(6).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("Status") = (CType(row.Cells(7).Controls(0), TextBox)).Text
Session("dt") = dt
GridView1.EditIndex = 1
BindData()
End Sub
Private Sub BindData()
GridView1.DataBind()
End Sub
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…