2 Replies - 1841 Views - Last Post: 13 July 2011 - 02:40 PM Rate Topic: -----

#1 Alexanderj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 07-July 11

problem filtering datagridview rows

Posted 13 July 2011 - 02:09 PM

Dear DIC's ;-)

im working on a program in which a user has to be able to filter a datagridview thats populated with words from a dictonaryfile in txt format.

the program has a input field where the user can input a filter, the inputfield(textbox)
in this case the filter is in the folowing format:
%filter keyword%


so i wrote a function for it, however im having the folowing problem: it does filter some values but it doesnt remove all false entries so i have to press the filter button multiple times in order to get rid of all rows not containing the filter word.


the code:

   private bool kwfilter(DataGridView dgview, string filter)
   {
       if (filter.Contains("%") == true) //look if the filterstring does contain the % parameter used for this type of filtering.
       {
           try
           {
               string[] tmp = filter.Split(char.Parse("%"));
               string containfilter = containfilter = tmp[1]; // so for example if filter is %monkey% then containfilter would be monkey
               
                   foreach (DataGridViewRow row in dgview.Rows)
                   {
                       if (row.Cells[0].Value.ToString().Contains(containfilter) == false)// if the row's first cell doesnt contain the value of string containfilter , then remove the row
                       {
                           dgview.Rows.Remove(row); 
                       }

                   }
               
           } catch(Exception e){
               MessageBox.Show(e.Message);
               return false;
           }

       }

       return false; // if the filter doesnt contain the filter parameter %  then just return false
       
       
   }


the code i use to call the function:

if (kwfilter(dtgrid_keywords, txtkwfilter_keyword.Text.ToString()) == true)
                {
                    MessageBox.Show("containfilter succesfully applied");
        
                }
                else
                {
                    MessageBox.Show("a error ocurred while applying the keyword filter, please check the syntax and try again");
           
                }


hope someone can explain why my function wont get rid of all bad entries in one run...

Thanks in advance,

Alexander Jeurissen

Is This A Good Question/Topic? 0
  • +

Replies To: problem filtering datagridview rows

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5503
  • View blog
  • Posts: 11,808
  • Joined: 02-June 10

Re: problem filtering datagridview rows

Posted 13 July 2011 - 02:27 PM

The big problem ther eis that you are using a ForEach loop and removing items from the collection in the process. That confuses the ForEach.

For example when you remove element 6, 7 becomes the new 6, 8 the new 7 and so on. But since your index value increments you totally skip over the old 7 because it became the new 6.

I suggest using a For loop, starting at the end and working backwards. If you start at the front and move forward you screw up your indexing in the same way when you remove an element.


for (int Index = dgView.Rows.Count; Index >= 0; Index--)
{
   // Do your thing
}

This post has been edited by tlhIn`toq: 13 July 2011 - 02:28 PM

Was This Post Helpful? 2
  • +
  • -

#3 Alexanderj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 07-July 11

Re: problem filtering datagridview rows

Posted 13 July 2011 - 02:40 PM

View PosttlhIn`toq, on 13 July 2011 - 02:27 PM, said:

The big problem ther eis that you are using a ForEach loop and removing items from the collection in the process. That confuses the ForEach.

For example when you remove element 6, 7 becomes the new 6, 8 the new 7 and so on. But since your index value increments you totally skip over the old 7 because it became the new 6.

I suggest using a For loop, starting at the end and working backwards. If you start at the front and move forward you screw up your indexing in the same way when you remove an element.


for (int Index = dgView.Rows.Count; Index >= 0; Index--)
{
   // Do your thing
}


that works..wonderfull

thx lesson learned dont edit the collection while using it ;-) in a forloop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1