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. Filter Gridview that are bound to an XML file

Filter Gridview that are bound to an XML file

Scheduled Pinned Locked Moved C#
databasesql-serversysadminxmlhelp
7 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.
  • R Offline
    R Offline
    Ronni Marker
    wrote on last edited by
    #1

    Hi everyone. I am currently playing around with XML files and gridview, and wondered if there were any easy way to filter data, so only rows with specific content are shown. I have the following.

            DataTable dt = new DataTable("Subscribers");
            dt.Columns.Add("ID");
            dt.Columns.Add("Email");
            dt.Columns.Add("signup");
            dt.Columns.Add("recent");
            dt.Columns.Add("Amount");
            dt.Columns.Add("Status");
            dt.ReadXml(Application.StartupPath + @"\\subscribers.xml");
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();
    

    and would like to filter it so rows who fx. only have the value 0 in the columns "Status" would be shown. Initially hoped that I could use dt.Select("Status == 0") but that didnt work. so any supercoders out there? yes, yes i did try to google my issue, but didnt find any solution to my small challenge here... other alternative that I will go back to my good old SQL Server, skip XML files and just use what I am used to - but that would be too easy...

    L 1 Reply Last reply
    0
    • R Ronni Marker

      Hi everyone. I am currently playing around with XML files and gridview, and wondered if there were any easy way to filter data, so only rows with specific content are shown. I have the following.

              DataTable dt = new DataTable("Subscribers");
              dt.Columns.Add("ID");
              dt.Columns.Add("Email");
              dt.Columns.Add("signup");
              dt.Columns.Add("recent");
              dt.Columns.Add("Amount");
              dt.Columns.Add("Status");
              dt.ReadXml(Application.StartupPath + @"\\subscribers.xml");
              dataGridView1.DataSource = dt;
              dataGridView1.Refresh();
      

      and would like to filter it so rows who fx. only have the value 0 in the columns "Status" would be shown. Initially hoped that I could use dt.Select("Status == 0") but that didnt work. so any supercoders out there? yes, yes i did try to google my issue, but didnt find any solution to my small challenge here... other alternative that I will go back to my good old SQL Server, skip XML files and just use what I am used to - but that would be too easy...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try it with dt.Select("Status = 0")

      R 1 Reply Last reply
      0
      • L Lost User

        Try it with dt.Select("Status = 0")

        R Offline
        R Offline
        Ronni Marker
        wrote on last edited by
        #3

        no, that didnt work either. (too bad though)

        L 1 Reply Last reply
        0
        • R Ronni Marker

          no, that didnt work either. (too bad though)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          See this example (it works):

          DataTable dataTable = new DataTable("MyTable");
          dataTable.Columns.Add("Column1");

          DataRow dataRow = dataTable.NewRow();
          dataRow["Column1"] = "0";
          dataTable.Rows.Add(dataRow);

          DataRow[] foundRows = dataTable.Select("Column1 = 0");
          Trace.Write(foundRows); // <- Count = 1

          Have you checked your dataTable ? Are there rows with content 0 in Status ?

          R 1 Reply Last reply
          0
          • L Lost User

            See this example (it works):

            DataTable dataTable = new DataTable("MyTable");
            dataTable.Columns.Add("Column1");

            DataRow dataRow = dataTable.NewRow();
            dataRow["Column1"] = "0";
            dataTable.Rows.Add(dataRow);

            DataRow[] foundRows = dataTable.Select("Column1 = 0");
            Trace.Write(foundRows); // <- Count = 1

            Have you checked your dataTable ? Are there rows with content 0 in Status ?

            R Offline
            R Offline
            Ronni Marker
            wrote on last edited by
            #5

            Yes, well came to the conclusion that the error for me is that I thought it would be enough to add a line into the code and that would be that. But can see that I need to do more than that. Basically tried to add

            dt.Select("Status =1");

            after reading the XML file and also tried to filter it like this

            dataGridView1.DataSource = dtSelect("Status =1");

            <- this actually gave an interesting output. But seems that I wouldnt be able to get away with only a line of code thist time here. So what would be the best way to do this? (just imagine that I have an XML file with more than 2000 Rows)

            H 1 Reply Last reply
            0
            • R Ronni Marker

              Yes, well came to the conclusion that the error for me is that I thought it would be enough to add a line into the code and that would be that. But can see that I need to do more than that. Basically tried to add

              dt.Select("Status =1");

              after reading the XML file and also tried to filter it like this

              dataGridView1.DataSource = dtSelect("Status =1");

              <- this actually gave an interesting output. But seems that I wouldnt be able to get away with only a line of code thist time here. So what would be the best way to do this? (just imagine that I have an XML file with more than 2000 Rows)

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              No guarantees that this will work but you could try adding a BindingSouce component to your form and then the bindings become

              datagrid.DataSource = bindingSource1;
              bindingsource1.DataSource = dt;

              BindingSource has a Filter property that you can use. In fact, if you search MSDN for BindingSource.Filter, the example uses XML which should make getting your stuff to work a bit easier. :)

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              R 1 Reply Last reply
              0
              • H Henry Minute

                No guarantees that this will work but you could try adding a BindingSouce component to your form and then the bindings become

                datagrid.DataSource = bindingSource1;
                bindingsource1.DataSource = dt;

                BindingSource has a Filter property that you can use. In fact, if you search MSDN for BindingSource.Filter, the example uses XML which should make getting your stuff to work a bit easier. :)

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                R Offline
                R Offline
                Ronni Marker
                wrote on last edited by
                #7

                Fantastic, worked first time - ehm after I remembered that there is a difference between 1 and '1'. But thanks for the headsup on BindingSource. Cheers,

                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