【问题标题】:Change Colour of Cells in DataGridView according to current/expired Time only仅根据当前/过期时间更改 DataGridView 中单元格的颜色
【发布时间】:2013-10-15 01:56:27
【问题描述】:

我正在使用从 excel 文件源填充的 datagridview。

我有一个“时间”列。我想更改“时间”列中单元格的颜色,以便过期时间(单元格)为灰色,下一个可用时间(单元格)为绿色等。

我觉得它比我想象的要复杂,因为在 Excel 中输入的时间也代表一个日期,尽管目前它只输入为 hh:mm AM/PM 格式。 DateTime.Now 还会显示系统日期 + 时间。

例子:

现在是晚上 11 点(当前时间,在此之前的任何时间都已过期)。 “时间”列中的单元格的值早于和晚于当前时间。晚上 10:52 及之前的时间现在都已过期,晚上 11:50 及之后的时间段都是可用的时间段。

   public Form1()
    {
        InitializeComponent();
        dataGridView1.CellPainting += dataGridView1_CellPainting;
    }

//////////////////////////////

   private void loadListBox4()
    {
        System.Data.OleDb.OleDbConnection MyConnection;
        System.Data.DataSet DtSet;
        System.Data.OleDb.OleDbDataAdapter MyCommand;
        MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Users\Dell\Documents\BusTimingExcel.xls;Extended Properties=Excel 8.0;");
        MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
        //MyCommand.TableMappings.Add("Route", "Location");
        DtSet = new System.Data.DataSet();
        MyCommand.Fill(DtSet);
        dataGridView1.DataSource = DtSet.Tables[0];
        MyConnection.Close();

        dataGridView1.Columns["Time"].DefaultCellStyle.Format = "t";
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.AllowUserToDeleteRows = false;
        dataGridView1.AllowUserToOrderColumns = true;
        dataGridView1.ReadOnly = true; ;

        DataView dv;
        dv = new DataView(DtSet.Tables[0], "Station = 'Poets Estate, The Dove'", "Time", DataViewRowState.CurrentRows);
        dataGridView1.DataSource = dv;

    }
}

【问题讨论】:

  • 您的Time 列实际上有什么数据类型?
  • dataGridView1.Columns["Time"].DefaultCellStyle.Format = "t"; //格式为hh:mm
  • 什么是过期时间?您必须将单元格中显示的时间与过期时间进行比较,以确定单元格的颜色。
  • 对不起,我已经用示例编辑了帖子。过期时间实际上是当前时间。 “NOW”之前的任何内容都已过期 NOW 之后的任何内容都可用
  • 您查看过我的解决方案了吗?如果它不起作用或有不清楚的地方,请发表评论

标签: c# winforms datagridview


【解决方案1】:

您可以尝试将代码添加到CellPainting 事件处理程序并在那里更新Cell BackColor

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){
  if(e.Value == null||
     e.ColumnIndex < 0 || e.RowIndex < 0 ||
     dataGridView1.Columns[e.ColumnIndex].Name!="Time") return;
  e.CellStyle.BackColor = ((DateTime)e.Value).TimeOfDay < DateTime.Now.TimeOfDay ?
                          Color.Gray : Color.Green;
}
//To register the event handler for CellPainting, you can use this code
dataGridView1.CellPainting += dataGridView1_CellPainting;//Place this in your form constructor

更新

为防止闪烁,请尝试以下代码(放置在表单构造函数中):

typeof(Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic |
             System.Reflection.BindingFlags.Instance).SetValue(dataGridView1, true, null);

【讨论】:

  • 这让我有点困惑。正如您在上面的代码中看到的那样,我使用了一种粗略的方法。我试过你的方法,但我不清楚我应该在哪里合并和调用这个事件。
  • @user970355 它是一个事件处理程序,你不调用它,你必须注册它。
  • 谢谢,但有些不对劲。它现在没有正确加载 datagridview。
  • @user970355 旧代码应该抛出异常,但你没有提到任何关于它的事情,我更新了代码,现在检查一下。 BTW,如果有问题,你应该给出一些清晰的描述,以便我可以改进代码
  • 谢谢。它有效,但仍然很慢。我可以看到生成了网格视图,并且单元格按预期着色,但界面非常慢,我必须上下移动滚动条,突然出现列和行,包括颜色。
【解决方案2】:

您在这里并没有给我们太多帮助,但是可以像这样实现在 gridview 中更改单元格的颜色:

dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;

现在弄清楚如何检查您的条件(“过期”、“可用”)并调用上面的代码。

希望对你有帮助,

克里斯

【讨论】:

  • 谢谢克里斯。对不起,但也许我没有清楚地解释自己。我可以将颜色更改为单元格,但正如您所说的那样,“过期”“可用”是我想要弄清楚的。谢谢
【解决方案3】:

如果您需要进一步的帮助,请告诉我,此代码可以以任何方式工作!

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

            if (((System.Windows.Forms.DataGridView)(sender)).Columns.Contains("Time"))
            {
                int Column_index = ((System.Windows.Forms.DataGridView)(sender)).Columns["Time"].Index;
                if ((e.ColumnIndex == Column_index) && (e.RowIndex != -1))
                {
                    DateTime Grid_Time = (DateTime)e.Value;
                    if (Grid_Time.TimeOfDay < DateTime.Now.TimeOfDay )
                    {
                        e.CellStyle.BackColor = Color.Gray;
                    }
                    else
                    {
                        e.CellStyle.BackColor = Color.Green;
                    }
                }
            }
        }

【讨论】:

  • 谢谢。此解决方案可以正常工作。我正在尝试找出“时间”列的数据类型。再次感谢。
  • 嗯,如果有人发现他的解决方案帮助了世界各地的许多程序员,那就太好了。如果您想找出数据类型,请检查 GetType()。抱歉回复晚了。我有几个小时不在办公室。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
相关资源
最近更新 更多