Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to create filtering for DataTable based on toggle switches?

How to create filtering for DataTable based on toggle switches?

Scheduled Pinned Locked Moved C#
algorithmstutorialquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    Hi, I have 15 toggle switches in my project (Using DevExpress). All of them are related to column 2 of the DataTable and are used for filtering. I want to filter the DataTable based on those toggle switches the user turns on. I don't know what algorithm should I use to set my filtering and cover all the toggle switches that are on.

    DataView dv = new DataView(my_table3);
    dv.RowFilter = $"Column2 = '{checked_item2[0]}' OR Column2 = '{checked_item2[1]}' OR Column2 = '{checked_item2[2]}'";

    OriginalGriffO J 2 Replies Last reply
    0
    • A Alex Dunlop

      Hi, I have 15 toggle switches in my project (Using DevExpress). All of them are related to column 2 of the DataTable and are used for filtering. I want to filter the DataTable based on those toggle switches the user turns on. I don't know what algorithm should I use to set my filtering and cover all the toggle switches that are on.

      DataView dv = new DataView(my_table3);
      dv.RowFilter = $"Column2 = '{checked_item2[0]}' OR Column2 = '{checked_item2[1]}' OR Column2 = '{checked_item2[2]}'";

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      I don't think string interpolation will work there: interpolated strings are a syntactic sugar for the older method using numbered parameters and are evaluated when the interpolated string is constructed, not when it is used:

      using System;

      public class Program
      {
      static string name = "???";
      static string xxx = $"Hello {name}!";
      static string yyy = "Hello {0}!";
      public static void Main()
      {
      name = "Paul";
      Console.WriteLine($"Hello {name}!");
      Console.WriteLine(xxx);
      Console.WriteLine(yyy, name);
      }
      }

      Will give you:

      Hello Paul!
      Hello ???!
      Hello Paul!

      So setting a RowFilter to the value of a variable doesn't mean that it will change the View at all whcn checks change: it will only use the values at the moment when the string is created. What you need to do is change the filter each time the user changes the check boxes.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        I don't think string interpolation will work there: interpolated strings are a syntactic sugar for the older method using numbered parameters and are evaluated when the interpolated string is constructed, not when it is used:

        using System;

        public class Program
        {
        static string name = "???";
        static string xxx = $"Hello {name}!";
        static string yyy = "Hello {0}!";
        public static void Main()
        {
        name = "Paul";
        Console.WriteLine($"Hello {name}!");
        Console.WriteLine(xxx);
        Console.WriteLine(yyy, name);
        }
        }

        Will give you:

        Hello Paul!
        Hello ???!
        Hello Paul!

        So setting a RowFilter to the value of a variable doesn't mean that it will change the View at all whcn checks change: it will only use the values at the moment when the string is created. What you need to do is change the filter each time the user changes the check boxes.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        A Offline
        A Offline
        Alex Dunlop
        wrote on last edited by
        #3

        This filter is a part of a bigger calculations. I export a huge Excel file into DataTable and filter rows based I toggle switches and continue the calculations. This process is done when the user clicks a button. So I don't need to have synchronous filtering. My problem is that I don't know how many toggle switches will be on to set them in my Filter String. Please guide me.

        OriginalGriffO 1 Reply Last reply
        0
        • A Alex Dunlop

          This filter is a part of a bigger calculations. I export a huge Excel file into DataTable and filter rows based I toggle switches and continue the calculations. This process is done when the user clicks a button. So I don't need to have synchronous filtering. My problem is that I don't know how many toggle switches will be on to set them in my Filter String. Please guide me.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          If you don't know, then you need to process them. Create a List<string> and loop through your switches: if it's set, add the condition string to the collection:

          if (myCheck.Checked)
          {
          myList.Add($"Column2 = '{checked_item2[n]}'");
          }

          Then when you have them all, use string.Join[^] with a separator of " OR " to produce your filter.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          A 1 Reply Last reply
          0
          • A Alex Dunlop

            Hi, I have 15 toggle switches in my project (Using DevExpress). All of them are related to column 2 of the DataTable and are used for filtering. I want to filter the DataTable based on those toggle switches the user turns on. I don't know what algorithm should I use to set my filtering and cover all the toggle switches that are on.

            DataView dv = new DataView(my_table3);
            dv.RowFilter = $"Column2 = '{checked_item2[0]}' OR Column2 = '{checked_item2[1]}' OR Column2 = '{checked_item2[2]}'";

            J Offline
            J Offline
            jsc42
            wrote on last edited by
            #5

            Might be slightly more compact to do

            Column2 IN ('{checked_item2.Join("', '")}')"

            this would also be impervious to changing the no of checkboxes I've not tested this. I am assuming that the filter texts are fixed (i.e. the user cannot change them). Also beware if any values have an ' in them - the ' have to be doubled.

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              If you don't know, then you need to process them. Create a List<string> and loop through your switches: if it's set, add the condition string to the collection:

              if (myCheck.Checked)
              {
              myList.Add($"Column2 = '{checked_item2[n]}'");
              }

              Then when you have them all, use string.Join[^] with a separator of " OR " to produce your filter.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              A Offline
              A Offline
              Alex Dunlop
              wrote on last edited by
              #6

              How can I join List<> elements parametrically? For example I don't know the count of checked toggle switches at first but during the process I find out that n = 5. Sting.Join(" OR ", checked_item2[0], .....);

              OriginalGriffO 1 Reply Last reply
              0
              • A Alex Dunlop

                How can I join List<> elements parametrically? For example I don't know the count of checked toggle switches at first but during the process I find out that n = 5. Sting.Join(" OR ", checked_item2[0], .....);

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                String.Join accepts any IEnumerable - including a List - so read what I said, and give it a try. To do it with parameters would mean knowing what your user was going to select at compile time ...

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                A 2 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  String.Join accepts any IEnumerable - including a List - so read what I said, and give it a try. To do it with parameters would mean knowing what your user was going to select at compile time ...

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                  A Offline
                  A Offline
                  Alex Dunlop
                  wrote on last edited by
                  #8

                  I tried this:

                  foreach (BarItem c in ribbonControl1.Items)
                  {
                  if (c is BarToggleSwitchItem tg2 && tg2.Checked)
                  {
                  if (tg2.Description == "text1" || tg2.Description == "text2" || tg2.Description == "text3" ||
                  tg2.Description == "text4" || tg2.Description == "text5" || tg2.Description == "text6")
                  {

                                      checked\_item2.Add(tg2.Description);
                                      checkedIndex2.Add($"Column15 = '{checked\_item2\[n\]}'");
                                      n++;
                                  }
                              }
                          }
                  

                  And:

                  string filterString;
                  filterString= string.Join(" OR ", checked_item2[0]);//The problem is here. I don't know how many times I need to write checked_item2[1,2...,n]

                  I don't know how many times I need to write checked_item2[1,2...,n]

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    String.Join accepts any IEnumerable - including a List - so read what I said, and give it a try. To do it with parameters would mean knowing what your user was going to select at compile time ...

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    A Offline
                    A Offline
                    Alex Dunlop
                    wrote on last edited by
                    #9

                    I used your algorithm and it solved my problem. Now, I want to deliver the filtered table into a new DataTable. I used this:

                    DataTable filteredTable = dv.ToTable();//dv is DataView

                    When I debug the code, there is some information in dv table but filteredTable is empty. Why?

                    OriginalGriffO 1 Reply Last reply
                    0
                    • A Alex Dunlop

                      I used your algorithm and it solved my problem. Now, I want to deliver the filtered table into a new DataTable. I used this:

                      DataTable filteredTable = dv.ToTable();//dv is DataView

                      When I debug the code, there is some information in dv table but filteredTable is empty. Why?

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      Firstly, a DataView isn't a table - it is a filtered view of a table and that's quite different in reality. Secondly, it works fine for me:

                          private void TwoDGVs\_Shown(object sender, EventArgs e)
                              {
                              string strConnect = SMDBSupport.SMInstanceStorage.GetInstanceConnectionString(DBName);
                              DataTable dt = new DataTable();
                              using (SqlConnection con = new SqlConnection(strConnect))
                                  {
                                  try
                                      {
                                      con.Open();
                                      using (SqlDataAdapter da = new SqlDataAdapter("SELECT \* FROM MyTable", con))
                                          {
                                          da.SelectCommand.Parameters.AddWithValue("@SEARCH", "The text to search for");
                                          da.Fill(dt);
                                          }
                                      }
                                  catch (Exception ex)
                                      {
                                      Debug.WriteLine(ex.ToString());
                                      }
                                  }
                              DataView dv = new DataView(dt);
                              Source.DataSource = dv;
                              }
                      
                          private void Filter\_TextChanged(object sender, EventArgs e)
                              {
                              if (Source.DataSource is DataView dv)
                                  {
                                  dv.RowFilter = $"Title LIKE '%{Filter.Text}%'";
                                  DataTable dt = dv.ToTable();
                                  Destination.DataSource = dt;
                                  }
                              }
                      

                      I get two DGV's, one looking a all rows, the other filtered.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups