2013-02-15 61 views
0

我需要在複選框列表中選中一個複選框後填充我的gridview。我試圖使用循環,如果用戶取消選中複選框,我還需要隱藏gridview。我使用一個SQL語句來提取數據。在SQL應該拉任何數據與選中複選框 「相關的節目,從保護小組chkbListControl_SelectedIndexChanged(發送者爲對象,E作爲System.EventArgs)選擇類別 書籍手柄chkListControl.SelectedIndexChanged我從複選框列表中選擇一個複選框後需要填充我的gridview

Dim sqlChecked As String = "select * " _ 
           & "from Books, Categories " _ 
           & "where Categories.CategoryCode=Books.CategoryCode " _ 
           & "order by Title;" 

    Dim sqlUnChecked As String = "select * from Books where Categories.CategoryCode=Books.CategoryCode;" 

    Dim selectedIndex As Integer = chkListControl.SelectedIndex 
    Dim i As Integer 

    If (selectedIndex <> -1) Then 

     For i = 0 To chkListControl.Items.Count - 1 

      If chkListControl.Items(i).Selected Then 

       gvwBookList.DataSource = ReturnTable(sqlChecked) 
       gvwBookList.DataBind() 

      Else 

       gvwBookList.DataSource = ReturnTable(sqlUnChecked) 
       gvwBookList.DataBind() 
       gvwBookList.Visible = False 

      End If 
     Next 
    End If 

End Sub 

回答

0

您需要修改像這樣sqlchecked

dim selectedcategories as string="" 
    If (selectedIndex <> -1) Then 

    For i = 0 To chkListControl.Items.Count - 1 

     If chkListControl.Items(i).Selected Then 

     if selectedcategories="" then 
      selectedcategories=chkListControl.Items(i).value 
     else 
      selectedcategories &="," & chkListControl.Items(i).value 
     end if 

     End If 
    Next 
End If 

dim strSQL as string="" 
      if selectedcategories.trim<>"" then 
        strSQL = "select * " _ 
          & "from Books, Categories " _ 
          & "where Categories.CategoryCode=Books.CategoryCode and      Categories.CategoryCode in (" & selectedcategories & ")" _ 
          & "order by Title;" 
    else 
strSQL="select * from Books where Categories.CategoryCode=Books.CategoryCode;" 

    end if 

      gvwBookList.DataSource = ReturnTable(strSQL) 
      gvwBookList.DataBind() 

您可以根據您的要求修改上述代碼。