0

How would I hide all rows on my dataGridView that do not match the date of "27/10/2013" by comparing it with whatever is in the DatePaid column. I have checked my mdb database (which I'm pulling the data from) and DataPaid has a datatype of ShortText, so no conversion to DateTime is necessary. Currently the code below hides all my rows.

 public void viewOverdue_Click(object sender, EventArgs e)
    {
        viewOverdue.ForeColor = Color.Red;
        viewHistory.ForeColor = Color.Black;
        viewHire.ForeColor = Color.Black;
        viewRent.ForeColor = Color.Black;

        CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
        manager.SuspendBinding();
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (!string.Equals(row.Cells[0].Value.ToString(), "27/10/2013", StringComparison.OrdinalIgnoreCase))
            {
                row.Visible = false;
            }
            else
            {
                row.Visible = true;
            }
        }
        manager.ResumeBinding();
    }
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • Hi @KSdev, the question posted above was by a colleague of mine, I believe that he may have got the details wrong – methuselah Dec 12 '13 at 19:17
  • Sorry, they marked it as answered so I assumed it was correct. – KSdev Dec 12 '13 at 19:18
  • what is the datasource of the grid ? rather than playing with grid columns why don't you exclude the rows from the data source and re-bind again ? – Kurubaran Dec 12 '13 at 19:21
  • @AccessDenied how would you go about doing that? – methuselah Dec 12 '13 at 19:30
  • The data source is an access database - I'm pulling two tables from it (both tables have the exact same structure) and are loaded via the following: `private void Form1_Load(object sender, EventArgs e) { this.hireTableAdapter.Fill(this.databaseDataSet.Hire); this.rentTableAdapter.Fill(this.databaseDataSet.Rent); }` – methuselah Dec 12 '13 at 19:31
  • To debug this issue you could try and use a `MessageBox` showing the `row.Cells[0].Value.ToString()` for one iteration and make sure it is formatted as expected. – KSdev Dec 12 '13 at 19:43
  • Since you are binding a dataset you can iterate through the dataset.Tables[0] which is basically equivalent to iterating through the grid. and then check for date and delete the rows dataset.Tables[0].Rows[0].Delete(); – Kurubaran Dec 12 '13 at 19:44
  • @KSdev - thanks that did the trick. Turns out it needed to be `if (!string.Equals(row.Cells[0].Value.ToString(), "27/10/2013 00:00:00", StringComparison.OrdinalIgnoreCase))` – methuselah Dec 12 '13 at 20:31

0 Answers0