Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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# 如何在C.net中通过比较单元格日期和当前日期来更改Datagridview单元格的颜色?_C#_Mysql - Fatal编程技术网

C# 如何在C.net中通过比较单元格日期和当前日期来更改Datagridview单元格的颜色?

C# 如何在C.net中通过比较单元格日期和当前日期来更改Datagridview单元格的颜色?,c#,mysql,C#,Mysql,我是C新手,这是我在C语言上的第一个项目,我的后端是Mysql,我被困在一个特定的点上,我有一个表单,其中包含一个datagridview,它与数据库值绑定,其中我有一个列TNS_Date,它是产品的过期日期,我想知道,如果TNS_日期大于当前日期,那么该特定单元格的背景颜色应为红色,我该怎么做,请帮我解决这个问题 我正在使用winforms,到现在为止,我已经尝试过了 foreach (DataGridViewRow row in TNSFormdgv.Rows) { var now

我是C新手,这是我在C语言上的第一个项目,我的后端是Mysql,我被困在一个特定的点上,我有一个表单,其中包含一个datagridview,它与数据库值绑定,其中我有一个列TNS_Date,它是产品的过期日期,我想知道,如果TNS_日期大于当前日期,那么该特定单元格的背景颜色应为红色,我该怎么做,请帮我解决这个问题

我正在使用winforms,到现在为止,我已经尝试过了

foreach (DataGridViewRow row in TNSFormdgv.Rows)
{
    var now                =  DateTime.Now;
    var expirationDate     =  DateTime.Parse(row.Cells[15].Value.ToString());
    var OneMonthBefore     = expirationDate.AddDays(-30);
    if (now > OneMonthBefore && now < expirationDate)
    {
        row.DefaultCellStyle.BackColor = Color.Yellow;
    }
    else if (now > expirationDate)
    {
        row.DefaultCellStyle.BackColor = Color.Red;
    }
}
这是我的整个代码,请检查我哪里出错了

public partial class TNS_Subscription : Form
{
    public TNS_Subscription()
    {
        InitializeComponent();
        TNSSubscriptionForm();
    }
    private void TNSFormBackbtn_Click(object sender, EventArgs e)
    {
        new Form2().Show();
        this.Hide();
    }
    public void TNSSubscriptionForm()
    {
        string ConString = "datasource=localhost;port=3306;username=root;password=ajay";
        MySqlConnection conDataBase = new MySqlConnection(ConString);
        MySqlCommand cmdDatabase = new MySqlCommand("Select acmecmpdetails.Cmp_Number,acmecmpdetails.Cmp_Name,acmecmpdetails.Cmp_AdminId,acmecmpdetails.Cmp_Address1,acmecmpdetails.Cmp_Country1,acmecmpdetails.Cmp_State1,acmecmpdetails.Cmp_City1,acmecmpdetails.Cmp_PostalCode,acmecmpdetails.Contact_Person1,acmecmpdetails.LandLine_Number1,acmecmpdetails.MobileNumber,acmecmpdetails.Cntct_Emailid,acmetally_detail.TallySerial_No,acmetally_detail.Tally_Release,acmetally_detail.Tally_Edition,acmetally_detail.TNS_Date,acmetally_detail.Tally_PrefPartner,acmetally_detail.Tally_accountId from acmesolutionsdatabase.acmecmpdetails INNER JOIN acmesolutionsdatabase.acmetally_detail ON acmesolutionsdatabase.acmecmpdetails.TallySerial_No= acmesolutionsdatabase.acmetally_detail.TallySerial_No;", conDataBase);


        try
        {
            MySqlDataAdapter cddsda = new MySqlDataAdapter();
            cddsda.SelectCommand = cmdDatabase;
            DataTable dbdataset = new DataTable();
            cddsda.Fill(dbdataset);
            BindingSource bsource = new BindingSource();
            bsource.DataSource = dbdataset;
            TNSFormdgv.DataSource = bsource;
            cddsda.Update(dbdataset);
            for(int i=0;i<TNSFormdgv.Rows.Count;i++)
            {
                if (Convert.ToDateTime(TNSFormdgv.Rows[i].Cells["TNS_Date"].Value.ToString()) > System.DateTime.Now.Date)
                {
                     TNSFormdgv.Rows[i].Cells["TNS_Date"].Style.BackColor = System.Drawing.Color.Red;
                }

            }
        }    
        catch (Exception ex)
        {
             MessageBox.Show(ex.Message);
        }
    }
}

提前感谢

以下是有帮助的示例:

  private void Form2_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSourceChanged += dataGridView1_DataSourceChanged;

        List<MyClass> list = new List<MyClass>();

        list.Add(new MyClass { ID = 1, Name = "1", Date = DateTime.Now });
        list.Add(new MyClass { ID = 2, Name = "2", Date = DateTime.Now });
        list.Add(new MyClass { ID = 3, Name = "3", Date = DateTime.Now });
        list.Add(new MyClass { ID = 4, Name = "4", Date = DateTime.Now.AddDays(1) });

        dataGridView1.DataSource = list;

    }

    void dataGridView1_DataSourceChanged(object sender, EventArgs e)
    {
        // Loop through all the cells where date is in future
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if ((row.Cells[2].Value != null) && (DateTime)row.Cells[2].Value > DateTime.Now)
            {
                row.Cells[2].Style.BackColor = Color.Blue;
            }
        }
    }

如果您使用的是asp.net datagridview,请使用模板字段来执行此操作。您可以在此处找到有关模板字段的更多详细信息:

您只需执行以下操作:

for(int i=0;i<MyGridView.Rows.Count;i++)
{
    if (Convert.ToDateTime(MyGridView.Rows[i].Cells["TNS_Date"].Value.ToString()) > System.DateTime.Now.Date)
    {
        MyGridView.Rows[i].Cells["TNS_Date"].Style.BackColor = System.Drawing.Color.Red;
    }
}

我希望它能解决这个问题。

我假设它是在Windows窗体中,因为您正在使用datagridview

对于datagridview,需要遍历网格的每个单元格以找到正确的单元格并设置背景颜色

为此,我创建了以下代码

void loadGrid()
{
        DataTable dtData = new DataTable();
        dtData.Columns.Add("TNS_Date");

        DataRow dRow;

        clmDate.DataPropertyName = "TNS_Date";
        dRow= dtData.NewRow();

        dRow["TNS_Date"] = "20-01-2014";
        dtData.Rows.Add(dRow);

        dRow = dtData.NewRow();

        dRow["TNS_Date"] = "20-02-2014";
        dtData.Rows.Add(dRow);


        dRow = dtData.NewRow();

        dRow["TNS_Date"] = "20-03-2014";
        dtData.Rows.Add(dRow);

        dgvGrid.DataSource = dtData;


        for (int i = 0; i <= dgvGrid.RowCount - 1; ++i)
        {
            //Finding the right cell to change colour.
            if(DateTime.Parse(dgvGrid["TNS_Date", i].Value.ToString()) >= DateTime.Now.Date)
            dgvGrid[0, i].Style.BackColor = Color.Bisque;

        }

}

是asp.net还是winforms/wpf?你试了什么?添加mysql标签有什么意义?@Uriil OP提到了DataGridView。所以我猜它必须是Windows窗体。DataGridView是Windows窗体中的控件。对于web,有GridView,但是我们声明局部变量clmDateclmDate的地方是datagridview中的列名。如果要将datatable与datagridview绑定,则它将是datatable中所需的列名。当我在出现此错误时按下按钮打开包含datagridview的表单时,我仍然会得到未设置为对象实例的错误对象引用,因为循环未被执行对象引用未设置为对象的实例确保在表单构造函数中调用InitializeComponent。并用您的gridview名称替换MyGridView。在执行此代码之前,请确保您在TNSFormdgv中有一些数据。我在TNSFormdgv中有数据,先生
for(int i=0;i<MyGridView.Rows.Count;i++)
{
    if (Convert.ToDateTime(MyGridView.Rows[i].Cells[3].Value.ToString()) > System.DateTime.Now.Date)
    {
        MyGridView.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.Red;
    }
}
void loadGrid()
{
        DataTable dtData = new DataTable();
        dtData.Columns.Add("TNS_Date");

        DataRow dRow;

        clmDate.DataPropertyName = "TNS_Date";
        dRow= dtData.NewRow();

        dRow["TNS_Date"] = "20-01-2014";
        dtData.Rows.Add(dRow);

        dRow = dtData.NewRow();

        dRow["TNS_Date"] = "20-02-2014";
        dtData.Rows.Add(dRow);


        dRow = dtData.NewRow();

        dRow["TNS_Date"] = "20-03-2014";
        dtData.Rows.Add(dRow);

        dgvGrid.DataSource = dtData;


        for (int i = 0; i <= dgvGrid.RowCount - 1; ++i)
        {
            //Finding the right cell to change colour.
            if(DateTime.Parse(dgvGrid["TNS_Date", i].Value.ToString()) >= DateTime.Now.Date)
            dgvGrid[0, i].Style.BackColor = Color.Bisque;

        }