DataGridView更改单元格背景并恢复默认样式 [英] DataGridView change cells background and restore default style

查看:149
本文介绍了DataGridView更改单元格背景并恢复默认样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的DGV中,单击一个单元格后,我想用一些值更改同一列中所有单元格的背景色。单击另一个单元格后,上一个单元格(及其所有列)必须恢复默认样式

  int currCell = dgvLogHeader.CurrentCell。 ColumnIndex; 
字符串模式= 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 =颜色。棕色;
}
}

但是此后,单元格已更改背景和默认样式

解决方案

这是一种实现方法:

  private void dgvLogHeader_CellMouseClick(对象发送者,DataGridViewCellMouseEventArgs e)
{
foreach(dgvLogHeader.Columns中的DataGridViewColumn col)
{
if(col .DefaultCellStyle.BackColor!= Color.Empty)
col.DefaultCellStyle.BackColor = Color.Empty;
}
dgvLogHeader.Columns [e.ColumnIndex] .DefaultCellStyle.BackColor = Color.Gainsboro;
}

这首先将每列颜色重置为默认值( Color.Empty ),然后为当前列着色。



请注意,这不会重置您在个人设置中设置的任何颜色单元格



将需要设置的单元格重置为 Color.Empty



您可能想添加

  dgvLogHeader.ClearSelection(); 

清除对所选单元格的选择。



但是:如果您需要根据单元格的值分别决定颜色,则必须在单元格上循环。最好在 CellPainting 事件中完成此操作,因为将以一种优化的方式调用此事件,使其仅包括显示的单元格。请注意,它是在每个单元格的基础上调用的,因此您需要遵守 e.ColumnIndex e.RowIndex 值。



更新:现在您已经明确了要问的问题,确实需要循环所有或所有可见单元格。 / p>

以下是您可以调用的函数:

  private void markCells(DataGridView dgv,字符串模式)
{
dgv.SuspendLayout();

foreach(dgv.Rows中的DataGridViewRow行)
foreach(row.Cells中的DataGridViewCell单元)
cell.Style.BackColor = cell.Value.ToString()==模式?
Color.LightBlue:Color.Empty;
dgv.ResumeLayout();
//dgv.ClearSelection()
}

如果快速超过整个DGV;你可以这样称呼它从 Textbox.TextChange 事件中获取。



在设置颜色时会暂停布局,因此它应该快速且没有颜色闪烁。



如果只想搜索一列,则可以添加一个附加条件,例如:

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

并将其放入 CellClick 事件中


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.

解决方案

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

这篇关于DataGridView更改单元格背景并恢复默认样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆