在向gridview添加行时索引超出范围(Index out of range when adding row to gridview)

编程入门 行业动态 更新时间:2024-10-28 00:19:19
在向gridview添加行时索引超出范围(Index out of range when adding row to gridview)

在尝试向gridview添加空行时,我得到一个超出范围异常的索引。 这是背后的代码。 有更多列,我只是删除它们来缩短代码。

Private Sub AddNewGridRow() Dim rowIndex As Integer = 0 If ViewState("CurrentData") IsNot Nothing Then Dim dtCurrentData As DataTable = DirectCast(ViewState("CurrentData"), DataTable) Dim drCurrentRow As DataRow = Nothing If dtCurrentData.Rows.Count > 0 Then For i As Integer = 1 To dtCurrentData.Rows.Count Dim lblVoucher As Label = DirectCast(GridView1.Rows(rowIndex).Cells(1).FindControl("lblVoucher"), Label) drCurrentRow = dtCurrentData.NewRow() drCurrentRow("RecID") = i + 1 dtCurrentData.Rows(i - 1)("RecID") = lblVoucher rowIndex += 1 Next dtCurrentData.Rows.Add(drCurrentRow) ViewState("CurrentData") = dtCurrentData GridView1.DataBind() End If Else Response.Write("ViewState is null") End If SetPreviousData() End Sub Private Sub SetPreviousData() Dim rowIndex As Integer = 0 If ViewState("CurrentData") IsNot Nothing Then Dim dt As DataTable = DirectCast(ViewState("CurrentData"), DataTable) If dt.Rows.Count > 0 Then For i As Integer = 0 To dt.Rows.Count - 1 'Out of range exception happens here when trying to fill the previous data. Dim lblVoucher As Label = DirectCast(GridView1.Rows(rowIndex).Cells(1).FindControl("lblVoucher"), Label) lblVoucher.Text = dt.Rows(i)("Voucher").ToString() rowIndex += 1 Next End If End If End Sub

这是该专栏的aspx。

<asp:TemplateField HeaderText="Voucher" SortExpression="RecID"> <HeaderStyle HorizontalAlign="Center" Width="100px" /> <ItemStyle HorizontalAlign="Center" /> <ItemTemplate> <asp:Label ID="lblVoucher" runat="server" Text='<%#Eval("RecID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>

I'm getting an index out of range exception while trying to add an empty row to a gridview. This is the code behind. There are more columns, I just removed them to shorten the code.

Private Sub AddNewGridRow() Dim rowIndex As Integer = 0 If ViewState("CurrentData") IsNot Nothing Then Dim dtCurrentData As DataTable = DirectCast(ViewState("CurrentData"), DataTable) Dim drCurrentRow As DataRow = Nothing If dtCurrentData.Rows.Count > 0 Then For i As Integer = 1 To dtCurrentData.Rows.Count Dim lblVoucher As Label = DirectCast(GridView1.Rows(rowIndex).Cells(1).FindControl("lblVoucher"), Label) drCurrentRow = dtCurrentData.NewRow() drCurrentRow("RecID") = i + 1 dtCurrentData.Rows(i - 1)("RecID") = lblVoucher rowIndex += 1 Next dtCurrentData.Rows.Add(drCurrentRow) ViewState("CurrentData") = dtCurrentData GridView1.DataBind() End If Else Response.Write("ViewState is null") End If SetPreviousData() End Sub Private Sub SetPreviousData() Dim rowIndex As Integer = 0 If ViewState("CurrentData") IsNot Nothing Then Dim dt As DataTable = DirectCast(ViewState("CurrentData"), DataTable) If dt.Rows.Count > 0 Then For i As Integer = 0 To dt.Rows.Count - 1 'Out of range exception happens here when trying to fill the previous data. Dim lblVoucher As Label = DirectCast(GridView1.Rows(rowIndex).Cells(1).FindControl("lblVoucher"), Label) lblVoucher.Text = dt.Rows(i)("Voucher").ToString() rowIndex += 1 Next End If End If End Sub

And this is the aspx for that column.

<asp:TemplateField HeaderText="Voucher" SortExpression="RecID"> <HeaderStyle HorizontalAlign="Center" Width="100px" /> <ItemStyle HorizontalAlign="Center" /> <ItemTemplate> <asp:Label ID="lblVoucher" runat="server" Text='<%#Eval("RecID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>

最满意答案

您需要将代码放入调试中,并查看该语句的哪个部分导致异常。

您有两个可能的问题:

您没有足够的行来满足rowIndex要求。

您没有足够的单元来满足单元格(1)的要求。

我会尝试重写问题行,如下所示:

Dim lblVoucher As Label = Nothing If GridView1.Rows.Count < rowIndex Then Dim oRow As DataGridViewRow oRow = GridView1.Rows(rowIndex) If oRow.Cells.Count > 1 Then lblVoucher = TryCast(oRow.Cells(1).FindControl("lblVoucher"), Label) Else ' Do something here when you don't have cells End If Else ' Do something here when you don't have a row End If

当您没有预期的内容时,可以在else子句中设置断点或抛出异常或适用于您的应用程序的任何内容。

You need to put the code into debug and see which portion of the statement is causing the exception.

You have two possible problems:

You do not have enough rows to meet the rowIndex requirement.

You do not have enough cells to meet the cells(1) requirement.

I would try rewriting the problem line as follows:

Dim lblVoucher As Label = Nothing If GridView1.Rows.Count < rowIndex Then Dim oRow As DataGridViewRow oRow = GridView1.Rows(rowIndex) If oRow.Cells.Count > 1 Then lblVoucher = TryCast(oRow.Cells(1).FindControl("lblVoucher"), Label) Else ' Do something here when you don't have cells End If Else ' Do something here when you don't have a row End If

You can set breakpoints or throw exceptions or whatever is appropriate for your application in the else clauses when you don't have what you are expecting.

更多推荐

本文发布于:2023-08-06 03:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   Index   gridview   row   adding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!