【问题标题】:GridView Checked Rows Values VB asp.netGridView 检查行值 VB asp.net
【发布时间】:2016-04-20 11:04:58
【问题描述】:

我有一个名为 GridViewTest 的 GridView,GridView 中的第一列是一个名为 CheckBox1 的模板字段复选框。我能够遍历 GridView 包含的所有行,并在名为 Label2 的标签中显示该值。我遇到的问题是,我只希望选中的行显示在 Label2 中。谁能帮帮我?

受保护的子 btnDisplay_Click(sender As Object, e As EventArgs) 处理 btnDisplay.Click

   Dim str As String = ""
   For i As Integer = 0 To GridViewTest.Rows.Count - 1
       str = (str + GridViewTest.Rows(i).Cells(0).Text & Convert.ToString(" >> ")) + GridViewTest.Rows(i).Cells(1).Text + " >> " + >GridViewTest.Rows(i).Cells(2).Text + "<br/>"
   Next

   Label2.Text = str

结束子

PS:这是 VB ASP.NET

<asp:GridView ID="GridViewTest" runat="server" 
                       AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
                       BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                       CssClass="GridviewTable" DataSourceID="SqlDataSourceDetailGrid" EnableTheming="True" 
                       Font-Names="Arial" Font-Size="8pt" ForeColor="Black" GridLines="Vertical" 
                       Height="100%" PageSize="15" TabIndex="25" Width="985px" AllowPaging="True" EnableModelValidation="True">
                       <FooterStyle BackColor="#CCCC99" />
                       <HeaderStyle BackColor="#6B696B" CssClass="Freezing" Font-Bold="True" 
                           ForeColor="#FFFFCC" Wrap="False" />
                       <RowStyle BackColor="#F7F7DE" VerticalAlign="Middle" Wrap="False" />
                       <AlternatingRowStyle BackColor="White" />
                       <Columns>

                           <asp:TemplateField>
                               <EditItemTemplate>
                                   <asp:CheckBox ID="CheckBox1" runat="server" />
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:CheckBox ID="CheckBox1" runat="server" />
                               </ItemTemplate>
                           </asp:TemplateField>

                           <asp:BoundField DataField="TestColumn1" HeaderText="TestColumn1" />
                           <asp:BoundField DataField="TestColumn2" HeaderText="TestColumn2" />
                           <asp:BoundField DataField="TestColumn3" HeaderText="TestColumn3" />
                           <asp:BoundField DataField="TestColumn4" HeaderText="TestColumn4" />
                           <asp:BoundField DataField="TestColumn5" HeaderText="TestColumn5" />
                           <asp:BoundField DataField="TestColumn6" HeaderText="TestColumn6" />

                       </Columns>
                       <PagerSettings PageButtonCount="20" Position="TopAndBottom" />
                       <PagerStyle BackColor="#F7F7DE" Font-Size="10pt" ForeColor="Black" 
                           HorizontalAlign="Center" VerticalAlign="Middle" />
                       <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

【问题讨论】:

  • 可以贴出gridview的标记吗?
  • 使用我在回答中提到的代码。只需将复选框的 id 替换为您正在使用的 id。

标签: asp.net vb.net gridview checkbox


【解决方案1】:

如果您使用的模板字段带有一个带有CheckBox1 id 的复选框,则在Page_PreRender 事件中使用下面的代码。

For Each row As GridViewRow In GridViewTest.Rows

   Dim result As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
   if result = True Then
      Label2.Text = string.Format("{0},row:{1} is {3}", Label2.Text , row.RowIndex, result);
   End If

Next

【讨论】:

  • 我得到一个错误:索引(零基数)必须大于或等于零并且小于参数列表的大小。请帮忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多