Locked C# datagrid range filtering

  • 10. dubna 2012 13:52
     
     
    Im stuck really bad. I have a datagrid with a colum of entry numbers. At the top I have two textboxes and I want to be able to enter two numbers so that it filters the datagrid to all records between them two numbers. My datagrid is not bound.   so eg. start number = 1   end number = 10 and it will filter the datagrid so that all the records between 1 and 10 show up. Can someone help me with this code, ive found a few examples bt they are either bound or datatimepickers :/ Im using C# code.

Všechny reakce

  • 10. dubna 2012 14:03
     
     
    Is whis a WinForm? webForm?
  • 10. dubna 2012 14:05
     
     

    Even with no answer, please check this links above

    http://stackoverflow.com/questions/6996375/how-to-filter-datagridview-using-a-textbox-in-c

    http://social.msdn.microsoft.com/Forums/br/csharpgeneral/thread/2c6c186c-4b31-4591-ac49-8eb6aa9ef584

  • 10. dubna 2012 14:28
     
      Obsahuje kód

    If you said your dgv is bound to any datasouce, then you can do a loop through all the rows and check if the value  of a current cell falls into your conditons. 

    I have created a list where values will be added into it, if they will meet the condistions.

    And then re-populate dgv:

            private void button1_Click(object sender, EventArgs e)
            {
                int a, b;
                if (int.TryParse(textBox1.Text, out a))
                {
                    if (int.TryParse(textBox2.Text, out b))
                    {
                        List<string> selected = new List<string>();
                        foreach (DataGridViewRow row in dataGridView1.Rows)
                        {
                            if (!row.IsNewRow)
                                if (int.Parse(row.Cells[0].Value.ToString()) >= a && int.Parse(row.Cells[0].Value.ToString()) <= b)
                                    selected.Add(row.Cells[0].Value.ToString());
                        }
                        dataGridView1.Rows.Clear();
                        foreach (string item in selected)
                            dataGridView1.Rows.Add(item.ToString());
                    }
                }
            }


    Mitja

  • 10. dubna 2012 16:12
     
     

    Hello,

    The best way is to filter the records from the database itself. Take the values in the textbox and provide that same search criteria in your query or sp.

  • 10. dubna 2012 22:03
     
     

    its in windows forms and ive serach the web for multiple answers n they mostly use datatimepickers or dnt link to the datagrid.

  • 10. dubna 2012 22:05
     
     
    its not bound.
  • 10. dubna 2012 22:06
     
     

    the info is already all displayed in the datagrid its jst so the user can get a specific group of that particular data. I've been able to do a singular filet on a datagrid i jst cant do it between two textbox entries T_T

  • 11. dubna 2012 12:10
     
     
    this piece of code dos work bt it only filters that particular colum and delted everythig else from each row :/ it just displays the filtered numbers and not the rest of the record???
  • 11. dubna 2012 12:19
     
     Odpovědět

    Can you show me an exact example of your, so I can see what do you have in mind by filtering (do some screen shorts) nad paste them here.

    and btw, images should show how it was before and after filtering (or you can do a decent explanation).

    My guess: you have plenty (more then one) columns, and you would like to filter dgv, by just qa single column, am i right?

    thx


    Mitja


  • 11. dubna 2012 12:24
     
     
  • 11. dubna 2012 12:25
     
     
  • 11. dubna 2012 12:25
     
     
    thats before and then after the filter as you can see you lose the values from the total colum :/
  • 11. dubna 2012 12:39
     
     

    Hi,

    I would second that. In particular even when you don't need or have a db you can create a DataSet/DataTable in memory so that you can bind to this. You have then all the data source features including filtering the source and seeing filtered data in your grid (or DataGridView). There is no difference at all then in programming this kind of stuff except that in one case the dataset/datatable was created for you by reading a database and in the other case you just created it programmatically yourself.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

  • 11. dubna 2012 13:00
     
     
    Sorry, but I dont see any actual filtering from those 2 images. You did write 2 "numbers" into textboxes - 010312 and 010812. But why all values from Total column disappear? I just dont get it?

    Mitja

  • 11. dubna 2012 13:34
     
     
    IT filtered the date coloum and gives all the results that are between the two dates entered bt then all the totals relating to those dates dissappear. maybe if i did a smmaller filter i think it hasnt shown coz i have limited entries. ill filter it smaller and add screen shot
  • 11. dubna 2012 13:35
     
     
  • 11. dubna 2012 13:36
     
     

    here is the smaller filter you can now see from all the data it has jst filtered to these few dates bt its not displaying the totals that belong to these dates.

  • 11. dubna 2012 14:02
     
     Odpovědět Obsahuje kód

    I got it now. Your data from column 2 disappears becuase you didnt "save" them and show them again.

    If you wanna do it properly, you have to "save" whole datagridviewrow, and as filtered show it back in dgv.

    Lets do some midifications on my code (form up there), which will do just that:

    private void button1_Click(object sender, EventArgs e)
            {
                int a, b;
                if (int.TryParse(textBox1.Text, out a))
                {
                    if (int.TryParse(textBox2.Text, out b))
                    {
                        List<DataGridViewRow> selectedRows = new List<DataGridViewRow>();
                        foreach (DataGridViewRow row in dataGridView1.Rows)
                        {
                            if (!row.IsNewRow)
                                if (int.Parse(row.Cells[0].Value.ToString()) >= a && int.Parse(row.Cells[0].Value.ToString()) <= b)
                                    selectedRows.Add(row);
                        }
                        dataGridView1.Rows.Clear();
                        foreach (DataGridViewRow row in selectedRows)
                            dataGridView1.Rows.Add(row);
                    }
                }
            }

    Hope it helps.


    Mitja

    • Označen jako odpověď Avidolly 11. dubna 2012 14:06
    •  
  • 11. dubna 2012 14:06
     
     

    Than you soooo much it works prefectly ^_^ My program is due at the end of next week and this was the last thing i was stuggling with xxx

  • 11. dubna 2012 14:17
     
     

    :) I am glad we went over barriers and missunderstandings, and fianlly found a solution.

    My program is due at the end of next week and this was the last thing i was stuggling with

    So do I get any credit? :D

    bye, bye


    Mitja


  • 12. dubna 2012 15:14
     
     

    yeah ^_^ i posted the link to this forum next to the pice of code you have given me hehe ^_^ i wont claim ur code as my own ^_^ thanks for all the help tho x

  • 20. dubna 2012 12:03
     
     
    Hi im back ive changed the textboxes to datetimepicked now to change it to a date search, How can i still filter between the two dates btu with datetimepickers insetad of teextboxes