简体   繁体   中英

Coloring Groups of Cells based on Content in Datagridview C#

I am working with a rather large datagrid bound to a Datatable. My goal is to color groups of cells based on data in the cell.

I want the datagrid to color all cells that contain a value, and then switch colors when it detects a new value and repeat this throughout the table.

Here's an example of what I'm working with in the table:

    Contract ID:
    123456 //Top of the contract, color all cells in the contract blue
    123456 //blue
    123456 //blue
    123456 //blue
    456789 //New contract, color all these cells green
    456789 //green 
    456789 //green
    987654 //Another new contract color all these blue (or another color) again
    etc... 

I have tried something similar to below but to no avail...

for (int i = 0; i < myDataGridView.Rows.Count; i++)
    {
         if (i > 0)
         {
             if (myDataGridView.Rows[i].Cells["contract_id"].Value != myDatagridView.Rows[i-1].Cells["contract_id"].Value)
             {
             myDataGridView.CurrentRow.Cells["contract_id"].BackColor = Color.Blue;
             }
         }
    }

I am not sure where to begin with this, I have tried looping over the rows and checking the values, but this ends up killing performance and speed and doesn't give me the result I am looking for. Any suggestions would be greatly appreciated.

If I understand your situation properly, you can achieve what you are looking for by handling the DataGridView.CellValueChanged event. This prevents you from having to loop through all your rows. In theory, this should work as you populate the DGV control.

Here is a very crude example of what I am talking about. You will likely need to play with it to make it work for your specific situation. In my case, it adjusts the Style.Backcolor of a cell when a changed value is committed. Since there is the possibility that there will only be a single row when data is entered, I set up a conditional to handle this case as well.

If this is the Winforms DGV control, you need to use the Cell.Style.BackColor Property, not the Cell.BackColor property (which doesn't exist on the Winforms DGV) in your code.

You will have to refine the code to suit your circumstances . . .

    private void Form1_Load(object sender, EventArgs e)
    {
        // Add a handler for the cell value changed event:
        this.myDataGridView.CellValueChanged += new DataGridViewCellEventHandler(myDataGridView_CellValueChanged);
    }

    void myDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        // grab a reference to the changed cell:
        DataGridViewCell cell = myDataGridView.Rows[e.RowIndex].Cells["contract_id"];

        // Guard against the case where this is the first row in the DGV table:
        if (cell.RowIndex - 1 >= 0)
        {
            if (cell.Value != myDataGridView.Rows[cell.RowIndex - 1].Cells["contract_id"].Value)
            {
                // CHange the Style.BackColor property for the cell:
                myDataGridView.CurrentRow.Cells["contract_id"].Style.BackColor = Color.Blue;
            }
        }

Definitively your only option is looping through the datatable rows since you do not have this color flag in your database.

I recommend you add a new datacolumn to your existing datatable, loop through it and set your color in it.

Then, you can colorize the cells in the "CellFormating" event. In that event you can read the value of the color column and use it.

There is a complete and simple example of the cellformating event here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM