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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19453017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:32:41  来源:igfitidea点击:

How to loop through all the rows of a gridview in all pages?

asp.netvb.netgridview

提问by barsan

I am using vb.net and I have a gridview which contains four columns including Textbox as a Itemtemplate field. This gridview is containing information of students and the textbox is the attendance status of the students for everytday. So, the requirement is to take the input from all of the textbox existing in the gridview as an input of student attendance for each student. Here, Paging is enabled because number or students can be more than 80 sometimes. But the problem is when I am looping through the gridview rows to get the textbox input it is only taking the first page values and the rest are left. I really need help on this. Any help is appreciated.

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

This is the GridView Code:

这是 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>

Here is the code behind of Save Button:

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

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

Thanks in advance.

提前致谢。

回答by Inside Man

You can use the Commands Below which I'm using it in my projects. Its logic is so simple, you go through all pages and in every page you go through all rows. You can also get your current page before doing this and after looping all you can get back there ;)

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

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

回答by Steve

You should never-ever (and this is a popular issue with new programmers) try to work with the data in the grid directly (ok almost never). You should work with the data you bound the grid to. This is the thing you set as the GridView1.Datasource = ?most likely in your page_load or some DataBinding event.

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

Assuming this is a datatable, you would want to do something like this:

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

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