locked
error in datagridview RRS feed

  • Question

  • Dear all , 

    In my data-grid view for one column called "Next calibration" i am using below code to highlight the cell if the date is over( compared to today) .

     private void dataGridViewforentryform_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                try
                {
                    var cellvalue = dataGridViewforentryform.Rows[e.RowIndex].Cells["nextCalibrationOnDataGridViewTextBoxColumn"].Value;
                    if (Convert.ToDateTime(cellvalue) < DateTime.Now)
                        dataGridViewforentryform.Rows[e.RowIndex].Cells["nextCalibrationOnDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;
                    dataGridViewforentryform.Rows[e.RowIndex].Cells["nextCalibrationOnDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
                }
                catch (Exception)
                {
    
                    //  throw;
                }
    
            }

    How ever if i select any other row in my data-grid view & try to close the form this below error is coming , please guide me how to solve this.



    • Moved by CoolDadTx Thursday, August 6, 2020 2:16 PM Winforms related
    Thursday, August 6, 2020 3:44 AM

All replies

  • Hi Ravi,

    Off the top of my head (not tested), something like this might work for you:

    private void dataGridViewforentryform_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        try
        {
            if (e.RowIndex >= 0)
            {
                var cellvalue = dataGridViewforentryform.Rows[e.RowIndex].Cells["nextCalibrationOnDataGridViewTextBoxColumn"].Value;
                if (cellvalue != DBNull.Value && Convert.ToDateTime(cellvalue) < DateTime.Now)
                    dataGridViewforentryform.Rows[e.RowIndex].Cells["nextCalibrationOnDataGridViewTextBoxColumn"].Style.ForeColor = Color.Red;
                dataGridViewforentryform.Rows[e.RowIndex].Cells["nextCalibrationOnDataGridViewTextBoxColumn"].Style.SelectionForeColor = Color.Red;
            }
        }
        catch (Exception)
        {
    
            //  throw;
        }
    


    }

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, August 6, 2020 4:46 AM
  • Thank you , Sorry this code didn't work not because of the code you gave , but i was looking at wrong method , i added a refresh method before exiting & now it is solved  .Thank you so much for your help.

    By the way could you please help with this question.??please..

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/310843b2-eba3-4d5d-866f-76f8845ef7d2/use-expression-in-winform-chart-y-series?forum=csharpgeneral#310843b2-eba3-4d5d-866f-76f8845ef7d2

    Thursday, August 6, 2020 5:02 AM
  • Hi,

    It sounds like Bonnie’s reply has solved your problem, please mark her reply as an answer to end this thread, and it will help other members to find the solution quickly if they face a similar issue

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 6, 2020 6:45 AM