C# 在 DevExpress GridView 上更改行颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18188525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Row Colours on DevExpress GridView
提问by 5tar-Kaster
I am trying to change the colour of a row to green in a devexpress gridview. When searching I found many examples of how to do this... in websites BUT this is a windows application. I cannot find any thing on a windows application so can someone please help me out a bit here.
我试图在 devexpress gridview 中将行的颜色更改为绿色。在搜索时,我发现了很多关于如何执行此操作的示例...在网站中,但这是一个 Windows 应用程序。我在 Windows 应用程序上找不到任何东西,所以有人可以在这里帮我一下。
I simply just want to change the colour of a single row to green.
我只是想将单行的颜色更改为绿色。
Forgot to mention, its a C# application.
忘了提,它是一个 C# 应用程序。
Thanks for the help.
谢谢您的帮助。
采纳答案by Giannis Paraskevopoulos
For changing the row colour in runtime handle the RowStyle event:
要在运行时更改行颜色,请处理 RowStyle 事件:
public Color color1;
public Color color2;
public int rowhandle;
private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
try
{
if (e.RowHandle == rowhandle)
{
if (color1 != null && color2 != null)
{
e.Appearance.BackColor = color1;
e.Appearance.BackColor2 = color2;
}
}
}
catch
{
}
}
private void button1_Click(object sender, EventArgs e)
{
color1 = Color.BurlyWood;
color2 = Color.DarkOrchid;
rowhandle = gridView1.FocusedRowHandle;
gridView1.RefreshRow(rowhandle);
}
The code bellow will maintain the colour:
下面的代码将保持颜色:
public partial class Form1 : Form
{
public Color color1;
public Color color2;
public int rowhandle;
public List<int> rowhandles;
private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
try
{
if (rowhandles.Any(x=>x==e.RowHandle))
{
if (color1 != null && color2 != null)
{
e.Appearance.BackColor = color1;
e.Appearance.BackColor2 = color2;
}
}
}
catch
{
}
}
private void button1_Click(object sender, EventArgs e)
{
color1 = Color.BurlyWood;
color2 = Color.DarkOrchid;
rowhandle = gridView1.FocusedRowHandle;
if (!rowhandles.Any(x => x == rowhandle))
rowhandles.Add(rowhandle);
gridView1.RefreshRow(rowhandle);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<int, string> l = new Dictionary<int, string>();
l.Add(1,"one");
l.Add(2,"two");
l.Add(3,"three");
l.Add(4, "four");
l.Add(5, "five");
l.Add(6, "six");
l.Add(7, "seven");
l.Add(8, "eight");
l.Add(9, "nine");
gridControl1.DataSource = l.ToList();
rowhandles = new List<int>();
}
}
回答by DmitryG
I suggest you use the following simple solution:
我建议您使用以下简单的解决方案:
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
//...
gridControl1.DataSource = new List<DataObj> {
new DataObj(){ ID=0, Name="A" },
new DataObj(){ ID=1, Name="B" },
new DataObj(){ ID=2, Name="C" },
new DataObj(){ ID=3, Name="D" },
};
gridView1.CustomDrawCell += gridView1_CustomDrawCell;
//...
void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
if(selectedRowHandle.GetValueOrDefault(GridControl.InvalidRowHandle) == e.RowHandle) {
e.Appearance.BackColor = Color.Green;
}
}
int? selectedRowHandle;
void button1_Click(object sender, EventArgs e) {
int prevSelectedRowHandle = selectedRowHandle.GetValueOrDefault(GridControl.InvalidRowHandle);
if(prevSelectedRowHandle != GridControl.InvalidRowHandle)
gridView1.RefreshRow(prevSelectedRowHandle); // reset row-style to default
selectedRowHandle = gridView1.FocusedRowHandle;
gridView1.InvalidateRow(gridView1.FocusedRowHandle); // row painting request
}
This solution is based on the GridView.CustomDrawCellevent that is preferred when it is only needed to change some specific row appearance. It also works for currently selected row rather then GridView.RowCellStyle.
此解决方案基于GridView.CustomDrawCell事件,当仅需要更改某些特定行外观时,首选该事件。它也适用于当前选定的行,而不是GridView.RowCellStyle。
Related help topic: Customizing Appearances of Individual Rows and Cells
相关帮助主题:自定义单个行和单元格的外观