如何更改当前DataGridView单元格的背景颜色?

huangapple go评论110阅读模式
英文:

How to change current DataGridView Cell Background color?

问题

我已经看到了关于这个主题的很多内容,但我的问题不同。

我有一个带有DataGrid视图的Windows窗体。我已将DataGrid视图的选择模式设置为FullRowSelection,因为我需要知道我在该GridView中的位置。我使用键盘箭头键在GridView中移动,现在我想要在使用箭头键在GridView上移动时更改当前单元格的背景颜色。

在这张图片中,你可以理解我的意思。

如何更改当前DataGridView单元格的背景颜色?

我已经检查了GridView的CurrentCellChanged事件,但不太理解它。

英文:

I have seen very topic with that subject, but my question is different.

I have a win form with a DataGrid view on it.I've put DataGrid view Selection Mode in FullRowSelection bcoz I need to know where am I in that gridview.I move in that gridview with keyboard arrow keys Now I want to change current cell back color when I move on grid view with arrow keys.

In this image you can understand my mean.

如何更改当前DataGridView单元格的背景颜色?

I have checked GridView CurrentCellChanged event but do not understand it.

答案1

得分: 1

由于您似乎希望在将 SelectionMode 设置为 FullRowSelect 时对 DataGridView 的 CurrentCell 进行着色,您可以处理 CellPainting 事件,并且当单元格是 CurrentCell 时,将 e.CellStyle 成员设置为在您的上下文中有意义的任何颜色组合。

e.CellStyle 的值是临时的,它会在单元格再次使用默认值进行绘制时重置,除非另有规定。

private void someDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
    var dgv = (sender as DataGridView);
    var cell = dgv[e.ColumnIndex, e.RowIndex];
    if (dgv.SelectionMode != DataGridViewSelectionMode.CellSelect && cell == dgv.CurrentCell) {
        // 设置您喜欢的任何颜色组合
        e.CellStyle.SelectionForeColor = Color.White;
        e.CellStyle.SelectionBackColor = Color.Red;
    }
}

您可能希望阅读这里的注释:
为什么我的 DataGridView 列没有按预期进行着色?
有关设置 DataGridView 样式及其层次关系的不同方法

英文:

Since it appears you want to colorize the DataGridView's CurrentCell when the SelectionMode is set to FullRowSelect, then you can handle the CellPainting event and, when the Cell is the CurrentCell, set the e.CellStyle member to any combination of colors that makes sense in your context.

The e.CellStyle value is transient, it's reset when the Cell is painted again using the default values, unless otherwise specified.

private void someDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex &lt; 0 || e.RowIndex &lt; 0) return;
    var dgv = (sender as DataGridView);
    var cell = dgv[e.ColumnIndex, e.RowIndex];
    if (dgv.SelectionMode != DataGridViewSelectionMode.CellSelect &amp;&amp; cell == dgv.CurrentCell) {
        // Set any color combination you prefer
        e.CellStyle.SelectionForeColor = Color.White;
        e.CellStyle.SelectionBackColor = Color.Red;
    }
}

You may want to read the notes here:
Why are my DataGridView columns not colorizing as they should?
about different ways of setting the DataGridView styles and their hierarchical relations

答案2

得分: 0

更改行选择颜色,您可以在Form_Load中添加以下代码:

dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;

随时提出任何问题。

英文:

to change the row selection color you can add on you Form_Load:

dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;

feel free to ask any question.

huangapple
  • 本文由 发表于 2023年6月8日 20:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定