0

How would I hide all rows on my dataGridView that do not match the date of "27/11/2013". Currently the code below hides all my rows...

private void viewOverdue_Click(object sender, EventArgs e)
{
    CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
    manager.SuspendBinding();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
            if (!string.Equals(row.Cells[0].Value.ToString(), "27/11/2013", StringComparison.OrdinalIgnoreCase))
            {
                row.Visible = false;
            }
            else
            {
                row.Visible = true;
            }
    }
    manager.ResumeBinding();
}
theshizy
  • 505
  • 3
  • 10
  • 31
  • You can use Filter, Take a look at this post It can help you : [Faster Method to Making DataGridViewRow's non-Visible](http://stackoverflow.com/questions/9663278/faster-method-to-making-datagridviewrows-non-visible) – Maryam Arshi Dec 12 '13 at 15:01
  • Are you sure that `string.Equals(row.Cells[0].Value.ToString()` is returning the same format as `"27/11/2013"`? – Hanady Dec 12 '13 at 15:03
  • @Hanady yep I'm certain – theshizy Dec 12 '13 at 15:16
  • @Maryam Arshi - how would this look like in the context of my code – theshizy Dec 12 '13 at 15:16
  • Looks like @Hanady was correct because if all rows are not visible then your cell values never equal the string you are comparing with. Are you sure its DD/MM/YYYY? – KSdev Dec 12 '13 at 15:23
  • Yep - would it be worth adding that the datatype in the mdb file is set to DateTime? – theshizy Dec 12 '13 at 15:25

1 Answers1

3

You should parse your DateTime to string and then compare.

DateTime dt = DateTime.ParseExact(row.Cells[0].Value.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string d = dt.ToString("dd/M/yyyy");
if (!string.Equals(d, "27/11/2013", StringComparison.OrdinalIgnoreCase)) {
    row.Visible = false;
}
else
{
    row.Visible = true;
}

Also implement the following namespace:

using System.Globalization;
Hanady
  • 779
  • 2
  • 15
  • 38
  • thanks for the response. This is what I have done so far: http://pastebin.com/mnYiK5f7. Getting the following error - `The name 'CultureInfo' does not exist in the current context` – theshizy Dec 12 '13 at 15:43
  • Check the updated answer. You have to add the namespace. – Hanady Dec 12 '13 at 16:21