仅在第一列(索引0)中显示单元格匹配特定日期的行 [英] Only displaying rows in first column (index 0) where cells match a particular date

查看:130
本文介绍了仅在第一列(索引0)中显示单元格匹配特定日期的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何隐藏dataGridView上与27/11/2013​​日期不匹配的所有行。目前下面的代码隐藏了我的所有行...

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();
}


推荐答案

你应该解析你的 DateTime 到字符串然后比较。

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;

这篇关于仅在第一列(索引0)中显示单元格匹配特定日期的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆