2013-02-26 37 views
0

我有一個帶有複選框的GridView。但是我在確定給定行的複選框是否被選中時遇到了實際問題。無法確定網格視圖中複選框的狀態

我需要從行中檢索某個值並將其放入代碼中。但是,當我遍歷GridView行時,程序不會輸入檢查checkBox'x狀態的if語句。

這裏是後端代碼:

Dim Primaryid As String = "Initial stage" 
    For Each gvr As GridViewRow In GridView1.Rows 

     If (CType(gvr.FindControl("CheckBox1"), CheckBox)).Checked = True Then 
      Primaryid = gvr.Cells(1).Text 
     End If 
    Next gvr 

    Dim exmess As String = "alert('" & Primaryid & "')" 
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", exmess, True) 

這裏是GridView控件的代碼。我在頁面加載時自動填充它:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
     GridLines="None" Width="1500px"> 
     <Columns> 

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

      </Columns> 
     <AlternatingRowStyle BackColor="White" /> 
     <EditRowStyle BackColor="#2461BF" /> 
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#EFF3FB" /> 
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
     <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
     <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
     <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
    </asp:GridView> 

如果您能指出我的錯誤,我將不勝感激。

回答

0

試試這個,它在winforms中起作用。不能保證它會爲你工作。我硬編碼了單元格的值,應該可以按照你的方式來獲取單元格。

無論如何,主要想法是檢查單元格的值,而不是試圖獲得「檢查」值(因爲它們會相同)。所以我基本上將單元格值解析爲一個布爾對象。

For Each r As DataGridViewRow In DataGridView1.Rows 
    Dim b As Boolean = False 
    Boolean.TryParse(r.Cells("CheckBox1").Value, b) 
    If b Then 
     'Do stuff 
    End If 
Next 

編輯:佬做的在C#中:F給我一秒鐘,我會後VB:P

+0

我剛剛試過,它仍然沒有得到我所選行的單元格值。它相當於處理所有的(如果我把它放在外面,如果if/else語句),或者如果它在裏面 – meks 2013-02-26 08:35:59

+0

爲什麼yo9u放置'Cells [3]'不是我們檢查總是出現在第一列的複選框的值網格視圖? – meks 2013-02-26 08:39:38

+0

如果你想要選擇的行應該是這樣的:GridView1.SelectedRows。這要求GridView將FullrowSelect作爲選擇模式。 – WozzeC 2013-02-26 08:41:39

0

試一下這個可以幫助ü。

Dim Primaryid As String = "Initial stage" 
    For Each row As GridViewRow In GridView1.Rows 
     Dim chk As CheckBox = TryCast(row.Cells(0).Controls(0), CheckBox) 

     If chk.Checked Then 
     End If 
    Next 

讓我知道,如果它的工作或沒有

1

你必須遍歷所有的行&找到你的CheckBox控件,然後檢查它的選中狀態。退房工作示例(我使用的在線轉換器轉換成VB)

Protected Sub btnGetSelectedRows_Click(sender As Object, e As EventArgs) 
Dim items = New StringBuilder() 
For Each grow As GridViewRow In GridView1.Rows 
    Dim chkTemp As CheckBox = DirectCast(grow.FindControl("chkSelectRow"), CheckBox) 
    If chkTemp IsNot Nothing Then 
     If chkTemp.Checked Then 
      items.Append(String.Format("{0},", GridView1.DataKeys(grow.RowIndex)("ProductID").ToString())) 
     End If 
    End If 
Next 

If items.Length > 0 Then 
     Response.Write("You selected Ids:" & Convert.ToString(items)) 
    End If 

End Sub 

而且ASPX

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
     AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ProductID"> 
     <Columns> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:CheckBox ID="chkSelectRow" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
       InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> 
      <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
       SortExpression="ProductName" /> 
      <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" 
       SortExpression="SupplierID" /> 
      <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
       SortExpression="CategoryID" /> 
      <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" 
       SortExpression="QuantityPerUnit" /> 
      <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
       SortExpression="UnitPrice" /> 
      <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" 
       SortExpression="UnitsInStock" /> 
      <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" 
       SortExpression="UnitsOnOrder" /> 
      <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" 
       SortExpression="ReorderLevel" /> 
      <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" 
       SortExpression="Discontinued" /> 
      <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
       SortExpression="CategoryName" /> 
     </Columns> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
     SelectCommand="SELECT * FROM [Alphabetical list of products]"></asp:SqlDataSource> 
    <br /> 

注意,我已經使用了

DataKeys

財產訪問該行的主鍵。我建議你在長期運行時使用cellIndex訪問單元格值時會發生同樣的錯誤,因爲將來您在gridview上更改列時會失敗。

達米安。