我在winform app(c#)中有GridView
和GridControl
,我在GridView
中有几列,我想设置标题列的背景。我可以做到,但它会改变所有相同颜色的列。我使用了那段代码:
gridcntrActic.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
gridcntrActic.LookAndFeel.UseDefaultLookAndFeel = false;
gridviewActiv.Appearance.HeaderPanel.Options.UseBackColor = true;
gridviewActiv.Appearance.HeaderPanel.BackColor = System.Drawing.Color.White;
此代码将所有列标题更改为白色。但是我想把第一列改为白色,然后将第二列改为红色等等。我该怎么做?
答案 0 :(得分:2)
如果要将第一列更改为白色,将第二列更改为红色,请更改它 这是一个例子:
gridcntrActic.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
gridcntrActic.LookAndFeel.UseDefaultLookAndFeel = false;
gridviewActiv.Columns[0].AppearanceHeader.Options.UseBackColor = true;
gridviewActiv.Columns[0].AppearanceHeader.BackColor = System.Drawing.Color.White;
gridviewActiv.Columns[1].AppearanceHeader.Options.UseBackColor = true;
gridviewActiv.Columns[1].AppearanceHeader.BackColor = System.Drawing.Color.Red;
//And so on.
答案 1 :(得分:1)
参考文档:Customizing the Appearances of Individual Column and Band Headers
自定义外观也可以分配给单个列和波段 头。该技术类似于上述技术。至 指定用于绘制单个列的外观设置 标题,使用列的GridColumn.AppearanceHeader属性。 band的模拟是GridBand.AppearanceHeader属性。
视图中所有列标题的外观设置由GridViewAppearances.HeaderPanel属性指定。 AppearanceHeader 属性为各个列标题提供了外观设置。
也可以使用GridView.CustomDrawColumnHeader事件自定义绘制列标题。
方法1:
gridControl1.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
gridControl1.LookAndFeel.UseDefaultLookAndFeel = false; // <<<<<<<<
gridviewActiv.Columns[0].Appearance.HeaderPanel.Options.UseBackColor = true;
gridviewActiv.Columns[0].Appearance.HeaderPanel.BackColor = System.Drawing.Color.White;
方法2:
void gridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) {
if (e.Column == null) return;
if (e.Column == colGrowth) //condition to paint specific column
{
e.Appearance.BackColor = Color.Red;
e.Handled = true; // must set flag to true to tell grid that it has been customized.
}
}
<强>来源:强> Applying Styles to Group Rows
例如:
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.Utils;
private void Form1_Load(object sender, System.EventArgs e) {
// ...
InitStyles();
}
private void InitStyles() {
GridView View = gridControl1.MainView as GridView;
// Customize the column headers' appearances.
AppearanceObject appCountry = View.Columns["Country"].AppearanceHeader;
appCountry.BackColor = Color.AntiqueWhite;
appCountry.BackColor2 = Color.Snow;
View.Columns["City"].AppearanceHeader.BackColor = Color.LightSalmon;
// Set the View's painting syle.
View.PaintStyleName = "UltraFlat";
}
注意:强>
使用“外观”设置,可以在GridControls的LookAndFeel属性设置为Flat,UltraFlat或Style3D值时更改列标题的背景颜色。如果要更改蒙皮列标题的颜色,则应创建自己的蒙皮。另一个解决方案是在CustomDrawColumnHeader事件处理程序中手动绘制列标题。此事件允许您根据需要绘制列标题,因此不依赖于网格的绘画。
答案 2 :(得分:0)
订阅活动CustomDrawColumnHeader并将颜色设置为您需要的列:
{{1}}