USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

Last post 05-08-2008 12:45 PM by codecorey. 8 replies.

Sort Posts:

  • USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-06-2008, 10:50 PM
    • Loading...
    • codecorey
    • Joined on 03-22-2008, 6:27 PM
    • Posts 21

     Hello,

    I need help with the code snippet below.  You'll notice that there two different checkbox controls....when they're clicked I would like for them to include a control to be used to filter the given data table, then view it.  With the following code I can do this successfully when only one of the two checkboxes are checked.  I have a feeling that there is a much easier and simple way to do this (switch statement perhaps....?).  Also... what would I need to add in order to make use both the ddList (ddlInstructor & ddlClasses) controls when both are checked?  I hope this makes sense!!     There will be a couple more checkboxes that will include more drop down or text box controls to filter this same table...

      

     SqlDataAdapter da = new SqlDataAdapter(strSQL2, strConn2);
            DataTable tblClass = new DataTable("ResultTable");
    
            da.Fill(tblClass);
            DataView vwClass = new DataView(tblClass);
    
            if (cbIncludeInstructor.Checked == true)
            {
    
                if (cbIncludeClass.Checked == false)
                {
                    vwClass.RowFilter = "Instructor = '" + ddlInstructors.SelectedItem + "'";
                }
            }
            else if (cbIncludeInstructor.Checked == false)
            {
                if (cbIncludeClass.Checked == true)
                {
                    vwClass.RowFilter = "ClassName = '" + ddlClasses.SelectedItem + "'";
                }
            }
    
            GridView2.DataSource = vwClass;
            GridView2.DataBind();
    
     
      
    										                
    										                
                									    
  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-07-2008, 2:22 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 7:10 AM
    • Croatia
    • Posts 1,148

    Try with this:

            if (cbIncludeInstructor.Checked)
            {
                vwClass.RowFilter = "Instructor = '" + ddlInstructors.SelectedItem + "'";
                if (cbIncludeClass.Checked)
                {
                    vwClass.RowFilter += " & ClassName = '" + ddlClasses.SelectedItem + "'";
                }
            }
            else if (cbIncludeClass.Checked)
            {
                vwClass.RowFilter = "ClassName = '" + ddlClasses.SelectedItem + "'";
            }

  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-07-2008, 8:53 AM
    • Loading...
    • Svante
    • Joined on 02-12-2007, 7:15 AM
    • Stockholm, Sweden
    • Posts 1,581
    • Moderator

     

    void AppendSelectionConditionally(StringBuilder s, string v, bool c)
    {
      if (!c)
      {
        return;
      }
      if (s.Length > 0)
      {
        s.Append(" & ");
      }
      s.Append(v);
    }
    
    // ...
    
    StringBuilder selection = new StringBuilder();
    AppendSelectionConditionally(selection, "Instructor = " + ddlInstructors.SelectedItem, cbIncludeInstructor.Checked);
    AppendSelectionConditionally(selection, "ClassName = " + ddlClasses.SelectedItem, cbIncludeClass.Checked);
    // ...
    vwClass.RowFilter = selection.ToString();
    
    // You must validate any input from the user!!! This code is to demonstrate the principle. It is not safe!
     
    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-07-2008, 9:13 AM
    • Loading...
    • codecorey
    • Joined on 03-22-2008, 6:27 PM
    • Posts 21

     Would this be its own method or something?  Or would I include this code in the same method where everything else is?

  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-07-2008, 9:24 AM
    • Loading...
    • Svante
    • Joined on 02-12-2007, 7:15 AM
    • Stockholm, Sweden
    • Posts 1,581
    • Moderator

    codecorey:
    Would this be its own method or something

    Yes, it'd be a private internal helper method in the class where the rest of the code is.

    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-08-2008, 11:18 AM
    • Loading...
    • codecorey
    • Joined on 03-22-2008, 6:27 PM
    • Posts 21

    When i run the code and select a name ("Charlie Smith", for example) it tells me that I'm missing operand after 'Smith'......  Why is this exception thrown  when I select this name out of the ddlInstructors?      The ddlnstructors list is attached to a datasource .... the first name and last name are joined like "'FirstName+' '+LastName AS Instructor...."     I tried another name in the list and its says missing operand after (whatever the instructor's last name is)    What am I missing?

  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-08-2008, 11:39 AM
    • Loading...
    • codecorey
    • Joined on 03-22-2008, 6:27 PM
    • Posts 21

     Ok, I think I fixed it with:

     

     StringBuilder selection = new StringBuilder();
            AppendSelectionConditionally(selection, "Instructor = '" + ddlInstructors.SelectedItem + "'", cbIncludeInstructor.Checked);
            AppendSelectionConditionally(selection, "ClassName = '" + ddlClasses.SelectedItem + "'", cbIncludeClass.Checked);
                
      

     Works fine when just ONE of the check boxes are checked  when I have BOTH checked then hit the search button an exception fires at the 'vwClass.RowFilter = selection.ToString();'   part...... says "The expression contains unsupported operator '&'. "
     

     


  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-08-2008, 12:32 PM
    Answer
    • Loading...
    • Svante
    • Joined on 02-12-2007, 7:15 AM
    • Stockholm, Sweden
    • Posts 1,581
    • Moderator

    codecorey:
    The expression contains unsupported operator '&'.

    That's because there is no such operator for this kind of expression. See http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(VS.80).aspx . Try 'AND' instead - I'm no ADO.NET guru, the code was just to demonstrate the principle.

    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: USING MULTIPLE CHECK BOX CONTROLS TO FILTER DATA......(HELP)

    05-08-2008, 12:45 PM
    • Loading...
    • codecorey
    • Joined on 03-22-2008, 6:27 PM
    • Posts 21

    'AND' seemed to of worked .......thanks 

Page 1 of 1 (9 items)