Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView中单元格颜色的动态变化_C#_Ms Access_Data Binding_Datagridview - Fatal编程技术网

C# DataGridView中单元格颜色的动态变化

C# DataGridView中单元格颜色的动态变化,c#,ms-access,data-binding,datagridview,C#,Ms Access,Data Binding,Datagridview,我正在开发一个数据库应用程序。其中一些列是文本列,一列是日期列。我想比较列的日期和系统日期,并想在列的日期值小于系统日期时将单元格背景颜色变为红色。下面给出的代码我已经试过了,但不起作用 private void EMIDGVAdm_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (this.EMIDGVAdm.Columns[e.ColumnIndex].DataProp

我正在开发一个数据库应用程序。其中一些列是文本列,一列是日期列。我想比较列的日期和系统日期,并想在列的日期值小于系统日期时将单元格背景颜色变为红色。下面给出的代码我已经试过了,但不起作用

private void EMIDGVAdm_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (this.EMIDGVAdm.Columns[e.ColumnIndex].DataPropertyName == "Date_1")
        {
            var EMIDate = Convert.ToDateTime(EMIDGVAdm.Rows[e.RowIndex].Cells["Date_1"].Value);
            if (EMIDate <= DateTime.Now)
            {
                e.CellStyle.BackColor = Color.Red;
                //e.CellStyle.ForeColor = Color.Red;
            }
        }
    }
private void EMIDGVAdm_CellFormatting(对象发送方,DataGridViewCellFormattingEventArgs e)
{
if(this.EMIDGVAdm.Columns[e.ColumnIndex].DataPropertyName==“Date_1”)
{
var EMIDate=Convert.ToDateTime(EMIDGVAdm.Rows[e.RowIndex].Cells[“Date_1”].Value);

if(EMIDate使用
今天
如果不需要时间部分。A
try catch
处理日期字段中可能的空值。必须在
var EMIDate
行中使用gridview列集合名称,这可能与DataPropertyName不同。因此,对于工作过程中的gridview,它们可能是相同的

分析了OP的项目。代码似乎无法从视图中读取列,因此必须加宽DataGridView和面板,以便在不滚动的情况下显示Date_1列。此外,DataPropertyName没有下划线。调整后的设计和代码有效:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Date 1")
        try
        {
            var EMIDate = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells["date1DataGridViewTextBoxColumn"].Value);
            if (EMIDate <= DateTime.Today)
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
        catch
        {
        }
}
private void dataGridView1\u单元格格式(对象发送方,DataGridViewCellFormattingEventArgs e)
{
如果(this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName==“日期1”)
尝试
{
var EMIDate=Convert.ToDateTime(dataGridView1.Rows[e.RowIndex]。单元格[“date1DataGridViewTextBoxColumn”]。值);

if(emidgDate)你调试代码了吗?调试器是否进入if条件?是的,它进入if。先生,我尝试了1个其他的是private void EMIDGVAdm_CellFormatting(对象发送者,DataGridViewCellFormattingEventArgs e){for(int I=0;ISystem.DateTime.Now.Date){EMIDGVAdm.Rows[i].Cells[5].Style.BackColor=System.Drawing.Color.Red;}好的。你现在用的是哪一个?以及它是否在条件内。我觉得e.CellStyle.BackColor应该起作用,我用的是与问题正文一起提供的。如果(Convert.ToDateTime(row.Cells[5].Value)try catch
来处理null字段。另外,返回到您的代码。我的版本根本不起作用。先生,您是否使用access DB尝试了上述代码,如果是的话比附加到我,因为我仍然没有得到回应。你可以给我发邮件或发送一些链接下载它。