【发布时间】: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