locked
Changing specific cell colour, not entire row based on a value RRS feed

  • Question

  • Hallo

    I'm trying to change just Cell[8] on specific row base on result from below, but I can only find the property ofr the row colour and not the specific cell.

    Any help will be much appreciated.

     private void dgvProductionData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                foreach (DataGridViewRow Myrow in dgvProductionData.Rows)
                {            //Here 2 cell is target value and 1 cell is Volume
                    if (Convert.ToDouble(Myrow.Cells[8].Value) < Convert.ToDouble(Myrow.Cells[7].Value))// Or your condition 
                    {                   
                        Myrow.DefaultCellStyle.BackColor = Color.Red; //Just want to change Cell[7] colour
                    }
                    else
                    { 
                        Myrow.DefaultCellStyle.BackColor = Color.Green; //Just want to change the Cell[7] colour
                    }
                }
            }


    labjac

    Tuesday, November 17, 2020 10:18 AM

All replies

  • Hello,

    There are several example, doing the entire row and doing a single cell shown below. I believe you want the second one.

    To change backcolor of row.

    If there is no DataSource you can get rid of the DataBoundItem checks

    private void CustomersDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == CustomersDataGridView.Columns["ColorComboBoxColumn"].Index && CustomersDataGridView.Rows[e.RowIndex].DataBoundItem != null)
        {
            var colorKeyValue = ((DataRowView)CustomersDataGridView.Rows[e.RowIndex].DataBoundItem).Row.Field<int>("ColorId");
            if (colorKeyValue == 2)
            {
                CustomersDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
            }
            else
            {
                CustomersDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Empty;
            }
        }
    }
    

    Change a specific cell color

    private void CustomersDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
    
        if (e.ColumnIndex == CustomersDataGridView.Columns["ColorComboBoxColumn"].Index && CustomersDataGridView.Rows[e.RowIndex].DataBoundItem != null)
        {
            var colorKeyValue = ((DataRowView)CustomersDataGridView.Rows[e.RowIndex].DataBoundItem).Row.Field<int>("ColorId");
            if (colorKeyValue == 2)
            {
                CustomersDataGridView.Rows[e.RowIndex].Cells[CustomersDataGridView.Columns["ColorComboBoxColumn"].Index].Style = new DataGridViewCellStyle { ForeColor = Color.White, BackColor = Color.Tomato };           
            }
            else
            {
                CustomersDataGridView.Rows[e.RowIndex].Cells[CustomersDataGridView.Columns["ColorComboBoxColumn"].Index].Style = null;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Tuesday, November 17, 2020 11:27 AM
  • Hi labjac,

    Thank you for posting here.

    If you provide data for the datagridview in this way:

       dataGridView1.DataSource = dataTable;

    Then there is one thing to note, DataGridView will have an empty row at the bottom for adding data by default.

    There is no data in this row, so when using e.Index to get data in the CellFormatting event, it will cause a NullReferenceException.

    You can add a judgment to prevent this.

                if (e.RowIndex < dataGridView1.Rows.Count - 1)
                { 
                    //......
                }

    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.

    Wednesday, November 18, 2020 6:36 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue. If you still have questions, please feel free to ask.

    Best Regards,

    Timon


    "Visual C#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Monday, November 23, 2020 9:42 AM