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 filter a List<> in a DataGridView?

How to filter a List<> in a DataGridView?

Scheduled Pinned Locked Moved C#
questiontutorial
2 Posts 2 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.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    Ive got a List<myClass> instance added to a DataGridView.DataSource. Now I want to filter this. What is the correct way to do so?

    OriginalGriffO 1 Reply Last reply
    0
    • T TMattC

      Ive got a List<myClass> instance added to a DataGridView.DataSource. Now I want to filter this. What is the correct way to do so?

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

      You don't - instead, you use a DataView object, and filter that, having set that as the DataSource. Here's my code for my music player list filter:

          /// <summary>
          /// DataView for files list and select
          /// </summary>
          private DataView dvFiles = new DataView();
      
              List<Track> tracks = GetTracks();
              tracks.Sort();
              DataTable dt = tracks.ToDataTable();
              dvFiles = new DataView(dt);
              dgvTracks.DataSource = dvFiles;
      
          /// <summary>
          /// A Filter changed - update the DataView
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void textBoxFilter\_TextChanged(object sender, EventArgs e)
              {
              StringBuilder sb = new StringBuilder();
              AppendFilter(sb, "TrackName", tbTrackFilter.Text, "");
              AppendFilter(sb, "Band", tbBandFilter.Text, " AND ");
              AppendFilter(sb, "Album", tbAlbumFilter.Text, " AND ");
              dvFiles.RowFilter = sb.ToString();
              }
      

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "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