【问题标题】:How to loop through all the rows of a gridview in all pages?如何遍历所有页面中gridview的所有行?
【发布时间】:2013-10-18 15:10:06
【问题描述】:

我正在使用 vb.net,并且我有一个 gridview,其中包含四列,其中包括 Textbox 作为 Itemtemplate 字段。该网格视图包含学生的信息,文本框是学生每天的出勤状态。因此,要求是将网格视图中存在的所有文本框的输入作为每个学生的学生出勤的输入。在这里,启用分页是因为人数或学生有时可能超过 80 人。但问题是,当我遍历 gridview 行以获取文本框输入时,它只获取第一页的值,其余的都留下了。我真的需要这方面的帮助。任何帮助表示赞赏。

这是 GridView 代码:

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" BorderColor="Black" BorderStyle="Solid" 
        CellPadding="4" Font-Bold="True" Font-Size="Small" ForeColor="#333333" 
        OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="20">
        <Columns>
            <asp:TemplateField HeaderText="No.">
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="crkod" HeaderText="Student ID" />
            <asp:BoundField DataField="crnama" HeaderText="Student Name" />
            <asp:TemplateField HeaderText="Attendance Status" 
                ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:TextBox ID="txtAttend" runat="server" BackColor="Control" MaxLength="1" 
                        Width="12px"></asp:TextBox>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle Font-Bold="True" />
    </asp:GridView>

这是保存按钮背后的代码:

For k As Integer = 0 To Me.GridView1.PageCount - 1
        Me.GridView1.PageIndex = k

        Dim rowNum As Integer = Me.GridView1.Rows.Count

        For i As Integer = 0 To rowNum - 1
            Dim tb As TextBox = DirectCast(GridView1.Rows(i).FindControl("txtAttend"), TextBox)
            attendSts = tb.Text


            studntID = GridView1.Rows(i).Cells(1).Text


            Sql = "INSERT INTO attendance (studentID,attendStatus,attendDate,courseID,yearsem,monthsem)" _
            & " VALUES (" _
             & "'" & studntID & "'," _
             & "'" & attendSts & "'," _
             & "'" & attnDate & "'," _
             & "'" & courseCode & "'," _
             & yearsem & "," _
             & monthsem & ")"

            CreateCommand(Sql, strConn)
        Next
    Next

提前致谢。

【问题讨论】:

  • 尝试在每个页面循环后绑定你的gridview
  • 感谢您的回复。我试过你的建议,但还是一样。还有其他想法吗?

标签: asp.net vb.net gridview


【解决方案1】:

您可以使用我在项目中使用的下面的命令。它的逻辑非常简单,您浏览所有页面,并且在每个页面中您浏览所有行。您还可以在执行此操作之前获取当前页面,然后在循环所有内容后返回该页面;)

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);

【讨论】:

    【解决方案2】:

    您永远不应该(这对于新程序员来说是一个流行的问题)尝试直接使用网格中的数据(好吧,几乎从不)。您应该使用将网格绑定到的数据。这是您最有可能在 page_load 或某些 DataBinding 事件中设置为 GridView1.Datasource = ? 的内容。

    假设这是一个数据表,你会想要做这样的事情:

    For each dr as datarow in ctype(GridView1.Datasource,DataTable).Rows
      attendSts = dr("Attend")
      studntID = dr("studntID")
      ..Your insert code here
    Next
    

    【讨论】:

    • 非常感谢您的有用回答。但是,您能否让我更清楚一点,如何在网格内的模板字段文本框中获取用户输入。我在这方面很新,所以我真的需要帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 2021-05-04
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2015-05-18
    相关资源
    最近更新 更多