尝试更新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>
答案 0 :(得分:3)
在你的for循环中缺少一个添加行的片段
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()
dt.Rows.Add(tableRow); // <-- this line is required
Next
答案 1 :(得分:0)
如果我没记错,可以在DataBind之后设置EditIndex。因为在此之前,没有索引1。 请试试。