2013-02-15 41 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() 

您可以根据您的要求修改上述代码。