Devexpress列标题颜色(每列不同颜色),winform c#

时间:2015-12-21 15:03:24

标签: c# winforms gridview devexpress

我在winform app(c#)中有GridViewGridControl,我在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;

此代码将所有列标题更改为白色。但是我想把第一列改为白色,然后将第二列改为红色等等。我该怎么做?

3 个答案:

答案 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}}