Export (0) Print
Expand All

DataGridTableStyle.HeaderBackColor Property

Gets or sets the background color of headers.

[Visual Basic]
Public Property HeaderBackColor As Color
[C#]
public Color HeaderBackColor {get; set;}
[C++]
public: __property Color get_HeaderBackColor();
public: __property void set_HeaderBackColor(Color);
[JScript]
public function get HeaderBackColor() : Color;
public function set HeaderBackColor(Color);

Property Value

A Color that represents the background color of headers.

Example

[Visual Basic] 
Private Sub Create_Table()
   ' Create DataSet.
   myDataSet = New DataSet("myDataSet")
   ' Create DataTable.
   Dim myCustomerTable As New DataTable("Customers")
   ' Create two columns, and add them to the table.
   Dim CustID As New DataColumn("CustID", GetType(Integer))
   Dim CustName As New DataColumn("CustName")
   myCustomerTable.Columns.Add(CustID)
   myCustomerTable.Columns.Add(CustName)
   ' Add table to DataSet.
   myDataSet.Tables.Add(myCustomerTable)
   dataGrid1.SetDataBinding(myDataSet, "Customers")
   myTableStyle = New DataGridTableStyle()
   myTableStyle.MappingName = "Customers"
   AddHandler myTableStyle.HeaderBackColorChanged, AddressOf HeaderBackColorChangedHandler
   AddHandler myTableStyle.HeaderForeColorChanged, AddressOf HeaderForeColorChangedHandler
End Sub 'Create_Table


' Change header background color.
Private Sub OnHeaderBackColor_Click(sender As Object, e As System.EventArgs) Handles button1.Click
   dataGrid1.TableStyles.Clear()
   Select Case comboBox1.SelectedItem.ToString()
      Case "Red"
         myTableStyle.HeaderBackColor = Color.Red
      Case "Yellow"
         myTableStyle.HeaderBackColor = Color.Yellow
      Case "Blue"
         myTableStyle.HeaderBackColor = Color.Blue
   End Select
   myTableStyle.AlternatingBackColor = Color.LightGray
   dataGrid1.TableStyles.Add(myTableStyle)
End Sub 'OnHeaderBackColor_Click


[C#] 
private void Create_Table()
{
   // Create DataSet.
   myDataSet = new DataSet("myDataSet");
   // Create DataTable.
   DataTable myCustomerTable = new DataTable("Customers");
   // Create two columns, and add them to the table.
   DataColumn CustID = new DataColumn("CustID", typeof(int));
   DataColumn CustName = new DataColumn("CustName");
   myCustomerTable.Columns.Add(CustID);
   myCustomerTable.Columns.Add(CustName);
   // Add table to DataSet.
   myDataSet.Tables.Add(myCustomerTable);
   dataGrid1.SetDataBinding(myDataSet,"Customers");
   myTableStyle = new DataGridTableStyle();
   myTableStyle.MappingName = "Customers";
   myTableStyle.HeaderBackColorChanged += 
      new System.EventHandler(HeaderBackColorChangedHandler); 
   myTableStyle.HeaderForeColorChanged +=
      new System.EventHandler(HeaderForeColorChangedHandler); 
}

// Change header background color.
private void OnHeaderBackColor_Click(object sender, System.EventArgs e)
{
   dataGrid1.TableStyles.Clear ();
   switch(comboBox1.SelectedItem.ToString())
   {
      case "Red":
         myTableStyle.HeaderBackColor = Color.Red;
         break;
      case "Yellow":
         myTableStyle.HeaderBackColor = Color.Yellow ;
         break;
      case "Blue":
         myTableStyle.HeaderBackColor = Color.Blue ;
         break;
   }
   myTableStyle.AlternatingBackColor = Color.LightGray;
   dataGrid1.TableStyles.Add(myTableStyle);
}

[C++] 
private:
    void Create_Table() {
        // Create DataSet.
        myDataSet = new DataSet(S"myDataSet");
        // Create DataTable.
        DataTable* myCustomerTable = new DataTable(S"Customers");
        // Create two columns, and add them to the table.
        DataColumn* CustID = new DataColumn(S"CustID", __typeof(int));
        DataColumn* CustName = new DataColumn(S"CustName");
        myCustomerTable->Columns->Add(CustID);
        myCustomerTable->Columns->Add(CustName);
        // Add table to DataSet.
        myDataSet->Tables->Add(myCustomerTable);
        dataGrid1->SetDataBinding(myDataSet, S"Customers");
        myTableStyle = new DataGridTableStyle();
        myTableStyle->MappingName = S"Customers";
        myTableStyle->HeaderBackColorChanged += 
            new System::EventHandler(this, &Form1::HeaderBackColorChangedHandler);
        myTableStyle->HeaderForeColorChanged += 
            new System::EventHandler(this, &Form1::HeaderForeColorChangedHandler);
    }

    // Change header background color.
    void OnHeaderBackColor_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
        dataGrid1->TableStyles->Clear();

        String* str = dynamic_cast<String*>(comboBox1->SelectedItem);
        if (str->Equals(S"Red"))
            myTableStyle->ForeColor = Color::Red;
        else if (str->Equals(S"Yellow"))
            myTableStyle->ForeColor = Color::Yellow;
        else if (str->Equals(S"Blue"))
            myTableStyle->ForeColor = Color::Blue;

        myTableStyle->AlternatingBackColor = Color::LightGray;
        dataGrid1->TableStyles->Add(myTableStyle);
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DataGridTableStyle Class | DataGridTableStyle Members | System.Windows.Forms Namespace | ShouldSerializeHeaderBackColor | ResetHeaderBackColor | HeaderForeColor | ColumnHeadersVisible | HeaderFont

Show:
© 2015 Microsoft