DataGridView change cells background and restore default style

霸气de小男生 提交于 2020-01-24 20:20:07

问题


In my DGV, after clicking on a cell, I want to change the background color of all the cells of the same column with some value. After clicking on another cell, the previous cell (and all its column) must restore default style

        int    currCell = dgvLogHeader.CurrentCell.ColumnIndex;
        string pattern = dgvLogHeader.CurrentCell.Value.ToString();
        dgvLogHeader.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ScrollBar;
        dgvLogHeader.DefaultCellStyle.BackColor = SystemColors.Info;

        for (int j=0; j < dgvLogHeader.Rows.Count; j++ ) {
            //dgvLogHeader.Columns[currCell].
            if (dgvLogHeader.Rows[j].Cells[currCell].Value.ToString() == pattern) {
                dgvLogHeader.Rows[j].Cells[currCell].Style.BackColor = Color.Brown;
            }
        }

But after that cells have changed background and the default style is lost.


回答1:


This is one way to do it:

private void dgvLogHeader_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    foreach (DataGridViewColumn col in dgvLogHeader.Columns)
    {
        if (col.DefaultCellStyle.BackColor != Color.Empty)
            col.DefaultCellStyle.BackColor = Color.Empty;
    }
    dgvLogHeader.Columns[e.ColumnIndex].DefaultCellStyle.BackColor = Color.Gainsboro;
}

This first resets each column with a color to default (Color.Empty) and then colors the current column.

Note that this will not reset any colors you have set in individual cells!

The reset those you need to set each to Color.Empty.

You may want to add a

dgvLogHeader.ClearSelection();

to clear the selection of the clicked cell.

But: If you need to decide on the colors on an individual basis, depending on Cell values then you will have to cycle over the Cells. This is best done in the CellPainting event as this is called in an optimized way to include only the shown cells. Note that it is called on a per cell basis, so you need to honor the e.ColumnIndex and e.RowIndex values..

Update: Now tht you have clarfied to question, indeed you need to loop oner either all or all visible cells..

Here is a function you could call to do so:

private void markCells(DataGridView dgv, string pattern)
{
    dgv.SuspendLayout();

    foreach (DataGridViewRow row in dgv.Rows)
        foreach (DataGridViewCell cell in row.Cells)
            cell.Style.BackColor = cell.Value.ToString() == pattern ?
                Color.LightBlue : Color.Empty;
    dgv.ResumeLayout();
    //dgv.ClearSelection()
}

If quickly goes over the whole DGV; you could call it e.g. from a Textbox.TextChange event.

It suspends layout while setting the colors, so it should be fast and without flicker..

If you only want to search in one column you can add an extra condition, maybe like this:

cell.Style.BackColor = cell.Value.ToString() == pattern &&  
                       cell.ColumnIndex == dgv.CurrentCell.ColumnIndex?

and also put it in a CellClick event



来源:https://stackoverflow.com/questions/50529612/datagridview-change-cells-background-and-restore-default-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!