GridView でデータを更新しようとして、例外エラーを受け取りました。もともと、私はGridView1.EditIndex = -1を持っていましたが、インデックス番号が負であってはならないことを読んだので、=1に変更しましたが、それでも機能しませんでした。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable("historyList")
dt.Columns.Add("TicketID", GetType(Integer))
dt.Columns.Add("DateCreated", GetType(DateTime))
dt.Columns.Add("FullName", GetType(String))
dt.Columns.Add("TicketType", GetType(String))
dt.Columns.Add("Subject", GetType(String))
dt.Columns.Add("Message", GetType(String))
dt.Columns.Add("Status", GetType(String))
For i = 0 To 6
Dim tableRow = dt.NewRow()
tableRow("TicketID") = i
tableRow("DateCreated") = Now()
tableRow("FullName") = i.ToString()
tableRow("TicketType") = i.ToString()
tableRow("Subject") = i.ToString()
tableRow("Message") = i.ToString()
tableRow("Status") = i.ToString()
Next
Session("dt") = dt
BindData()
End Sub
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(0).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("DateCreated") = (CType(row.Cells(1).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("FullName") = (CType(row.Cells(2).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("TicketType") = (CType(row.Cells(3).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("Subject") = (CType(row.Cells(4).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("Message") = (CType(row.Cells(5).Controls(0), TextBox)).Text
dt.Rows(row.DataItemIndex)("Status") = (CType(row.Cells(6).Controls(0), TextBox)).Text
Session("dt") = dt
GridView1.EditIndex = 1
BindData()
End Sub
Private Sub BindData()
GridView1.DataBind()
End Sub
だから、ここに私のaspxコードがあります:
<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>