如何遍历所有页面中gridview的所有行?

时间:2013-10-18 15:10:06

标签: asp.net vb.net gridview

我正在使用vb.net,我有一个gridview,其中包含四列,包括Textbox作为Itemtemplate字段。此gridview包含学生的信息,文本框是每个学生的出勤状态。因此,要求是将来自gridview中存在的所有文本框的输入作为每个学生的学生出勤输入。在这里,启用分页是因为数字或学生有时可能超过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

提前致谢。

2 个答案:

答案 0 :(得分:2)

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

//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);

答案 1 :(得分:0)

你永远不应该(这是新程序员的一个流行问题)尝试直接使用网格中的数据(好吧几乎从不)。您应该使用绑定到网格的数据。这是您在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