问题
I have a GridView
and GridControl
in winform app (c#) and I have several columns in GridView
and I want to set background of header columns. I can do it but it changes all columns in same color. I used that code:
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;
This code changes all column headers to white. But I want to change first column to white and second to red and so. How can I do it?
回答1:
If you want to change first column to white and second to red, so change it.
Here is example:
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.
回答2:
Reference documentation: Customizing the Appearances of Individual Column and Band Headers
Custom appearances can also be assigned to individual column and band headers. This technique is similar to the one described above. To specify the appearance settings used to paint an individual column header, use the column's GridColumn.AppearanceHeader property. The analogue for bands is the GridBand.AppearanceHeader property.
The appearance settings of all the column headers within the View are specified by the GridViewAppearances.HeaderPanel property. The AppearanceHeader property provides appearance settings for individual column headers.
Column headers can also be custom painted using the GridView.CustomDrawColumnHeader event.
Method 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;
Method 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.
}
}
Source:Applying Styles to Group Rows
Example:
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";
}
Note:
Using the Appearance settings, you can change the background color of a column header when GridControls's LookAndFeel property is set to Flat, UltraFlat or Style3D value. If you wish to change a color for a skinned column header, you should create your own Skin. One more solution is to paint the column header manually within the CustomDrawColumnHeader event handler. This event allows you to paint the column header just as you need and thus do not rely on the grid's painting.
回答3:
Subscribe to event CustomDrawColumnHeader and set color to column you need:
gridView1.CustomDrawColumnHeader +=
new ColumnHeaderCustomDrawEventHandler(gridView1_CustomDrawColumnHeader);
void gridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == _yourColumn)
{
e.Appearance.BackColor = Color.Red;
e.Handled = true;
}
}
来源:https://stackoverflow.com/questions/34398669/devexpress-column-header-color-each-column-different-color-winform-c-sharp