public partial class FrmEndDateSummaryOnLoad : FrmErpSimple

        List<string> mVehicleRemainderList;

        //SMdiForm mMdiParent;
        string mCurrentUserId;
        string mCurrentPeriodId;
        DataTable dataTableVehicleID = new DataTable();
        DataTable dataTableVehicleNextKMS = new DataTable();

        public FrmEndDateSummaryOnLoad(CErpDataConnection pErpDataConnection, CUserLoginInfo pUserLoginInfo
        , CRegional pRegional)
            : base(pErpDataConnection, pUserLoginInfo)


        protected override void OnLoad(EventArgs e)
            mCurrentPeriodId = mUserLoginInfo.GetPeriodID();
            mCurrentUserId = mUserLoginInfo.GetUserID();
            mCommand = mErpDataConnection.CreateCommand();
            mConnection = mErpDataConnection.CreateConnection();
           // dttxtDate = new SDateTimeMaskedTextBox();

        #region EndDateSummary

        CDataTable dataTable;
        public void SearchEndDate()
            if (SCUtil.IsDate(dttxtDate.Text))
               //same code as In Else {},on load date textbox is blank..
                string sql;
                string dt = mErpDataConnection.GetDBServerDateTime();
                dttxtDate.Text = dt;
                DateTime dtSearch = dttxtDate.uStringToDateTimeConversion();
                DateTime dtDateAfterAdd = dtSearch.AddDays(SCUtil.StringToIntAbsolute(mErpDataConnection.GetRegistryKeyValue("REMINDER_ADD_DAYS")));
                string strDtSearch = SCUtil.DateTimeToYYYYMMDD(dtDateAfterAdd);

                sql = "SELECT * FROM Rpt_Trans_End_Date_Summary"
                    + " WHERE ('" + strDtSearch + "' >=  Insurance_End_Date)"
                    + " OR ('" + strDtSearch + "' >=  Tax_End_Date)"
                    + " OR ('" + strDtSearch + "' >=  Fitness_End_Date)"
                    + " OR ('" + strDtSearch + "' >=  Permit_End_Date)";

                dataTable = new CDataTable();
                mCommand.CommandText = sql;
                mCommand.Connection = mConnection;
                dataTable = mErpDataConnection.FillDataTable(mCommand);
                dgvEndDateSummery.DataSource = dataTable;

                for (int i = 1; i < dgvEndDateSummery.Rows.Count; i++)
                    SetCellColor(i, dtDateAfterAdd, "Insurance_End_Date");
                    SetCellColor(i, dtDateAfterAdd, "Tax_End_Date");
                    SetCellColor(i, dtDateAfterAdd, "Fitness_End_Date");
                    SetCellColor(i, dtDateAfterAdd, "Permit_End_Date");

        public void SetCellColor(int pRowNumber, DateTime pDate, string pColumn)
            string strCellValue = dgvEndDateSummery.Rows[pRowNumber - 1].Cells[pColumn].Value.ToString();
            DateTime cellDate = SCUtil.StringToDateTimeConversion(strCellValue);
            //            DateTime dat = Convert.ToDateTime(dgvEndDateSummery.Rows[pRowNumber - 1].Cells[pColumn].Value.ToString());

            if (cellDate <= pDate)
                dgvEndDateSummery.Rows[pRowNumber - 1].Cells[pColumn].Style.BackColor = Color.Cyan;

Edited by jaimin4829

3 Years
Discussion Span
Last Post by Maligui

Have you tried a this.Update(); after the call to SearchEndDate();? This will force the form to redraw.

Edited by Mike Askew


Keep in mind that Form_Load event handler will run on a background thread, and it is not recommended when changing state of objects, but more or less to ready data for the form. More then likely there is an error happening and it is silently throwing an exception (which is the normal behaviour of Form_Load)

I would call the setcolor() method on the Constructor. Or, better yet - move the the entire Form_Load method to another method and call from the constructor.

Common practice is to wrap the method on a try...catch block and capture the error that could be causing the method to bail.

Votes + Comments
But it works on Btn_Click...

It properly works on btn_Click event..
Also at on_load debug..it enters in loop properly but can't show effect of changing color.!!

private void btnSearch_Click(object sender, EventArgs e)



Edited by jaimin4829


The btnSearch event runs on the same thread as your UI. So the method SearchEndDate() also runs on the UI thread. It is possible you have a Cross thread exception going on when using Form Load - meaning that the thread to update the UI is not running on the same thread as the UI.

The best way to code is to only do background threading on tasks that take a while to run (such as your call to the sql server data base). The Form_Load event handler is a background thread. I don't know why MS made it this was, but it is.

You can try something like this to update the color:

// Orignally this was ChangeCellColor()
public void CheckCellDate(int pRowNumber, DateTime pDate, string pColumn)
    string strCellValue = dgvEndDateSummery.Rows[pRowNumber].Cells[pColumn].Value.ToString();
    DateTime cellDate = Convert.ToDateTime(strCellValue);

    if (cellDate <= pDate) SetColor(pRowNumber, pColumn, Color.Cyan);

// Pattern to make thred safe calls in case you are on a 
// different thread than the one that made the control.
private void SetColor(int row, string column, Color color)
    if (dgvEndDateSummery.InvokeRequired)
        SetColorCallback d = new SetColorCallback(SetColor);
        this.Invoke(d, new object[] { row, column, color });
        dgvEndDateSummery.Rows[row].Cells[column].Style.BackColor = Color.Cyan;


On final note, is that the UI may take time to draw the value to the grid. On Form_Load, the data grid is filling the values, but may not have the value filled by the time the code is checking it. You can use the CellValueChanged event to run the check. And use a DateTime.TypeParse() for the cell value.

Edited by Maligui: Addition to post

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.