looking for some solutions? You are welcome.

SOLVED: how to compare datagridview cell date with system date and cell background turn red in c# and ms-access database

Abhi:

I am new to C#. I am working on a database application using C#. In which some columns are text column and one column is for date. I want to compare cell date with system date and want to turn cell background color Red. I search a lot and go through many answers but not fit in my case. Thanks in advance for help.

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

Here is the code which are working perfectly for cellformating event. but These do not have ms-access database.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LibraryApp
{
    public partial class Form1 : Form
    {
        private List<Book> _books;

        public Form1()
        {
            InitializeComponent();
            dgvBooks.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvBooks_CellFormatting);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _books = new List<Book>();
            _books.Add(new Book() { BookID = 1, Title = "C# 5.0 in a Nutshell: The Definitive Reference", Borrower = "John Smith", DateBorrow = new DateTime(2014, 3, 15), DateReturn = new DateTime(2014, 3, 22) });
            _books.Add(new Book() { BookID = 2, Title = "Pro ASP.NET MVC 5", Borrower = "Aaron Carter", DateBorrow = new DateTime(2014, 3, 10), DateReturn = new DateTime(2014, 3, 17) });
            _books.Add(new Book() { BookID = 3, Title = "Pro C# 5.0 and the .NET 4.5 Framework", Borrower = "Andrew Troelsen", DateBorrow = new DateTime(2014, 3, 19), DateReturn = new DateTime(2014, 3, 26) });
            _books.Add(new Book() { BookID = 4, Title = "C# in Depth, 3rd Edition", Borrower = "Jon Skeet", DateBorrow = new DateTime(2014, 3, 10), DateReturn = new DateTime(2014, 3, 17) });
            _books.Add(new Book() { BookID = 5, Title = "Murach's ASP.NET 4.5 Web Programming with C# 2012", Borrower = "Mary Delamater and Anne Boehm", DateBorrow = new DateTime(2014, 2, 10), DateReturn = new DateTime(2014, 2, 17) });
            _books.Add(new Book() { BookID = 6, Title = "Head First C#", Borrower = " Jennifer Greene and Andrew Stellman", DateBorrow = new DateTime(2014, 3, 20), DateReturn = new DateTime(2014, 3, 27) });
            _books.Add(new Book() { BookID = 7, Title = "CLR via C# (Microsoft, Developer Reference)", Borrower = "Jeffrey Richter", DateBorrow = new DateTime(2014, 3, 19), DateReturn = new DateTime(2014, 3, 26) });
            dgvBooks.DataSource = _books;
        }

        private void dgvBooks_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dgvBooks.Columns[e.ColumnIndex].DataPropertyName == "DateReturn")
            {
                var returnDate = Convert.ToDateTime(dgvBooks.Rows[e.RowIndex].Cells["DateReturn"].Value);
                if (returnDate < DateTime.Now)
                {
                    e.CellStyle.BackColor = Color.Red;
                    //e.CellStyle.ForeColor = Color.Red;
                }
            }
        }
    }
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent