Export (0) Print
Expand All

DataGridViewBand Class

Represents a linear collection of elements in a DataGridView control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public ref class DataGridViewBand : public DataGridViewElement, 
	ICloneable, IDisposable

The DataGridViewBand type exposes the following members.

  NameDescription
Public propertyContextMenuStripGets or sets the shortcut menu for the band.
Public propertyDataGridViewGets the DataGridView control associated with this element. (Inherited from DataGridViewElement.)
Public propertyDefaultCellStyleGets or sets the default cell style of the band.
Public propertyDefaultHeaderCellTypeGets or sets the run-time type of the default header cell.
Public propertyDisplayedGets a value indicating whether the band is currently displayed onscreen.
Public propertyFrozenGets or sets a value indicating whether the band will move when a user scrolls through the DataGridView.
Public propertyHasDefaultCellStyleGets a value indicating whether the DefaultCellStyle property has been set.
Protected propertyHeaderCellCoreGets or sets the header cell of the DataGridViewBand.
Public propertyIndexGets the relative position of the band within the DataGridView control.
Public propertyInheritedStyleGets the cell style in effect for the current band, taking into account style inheritance.
Protected propertyIsRowGets a value indicating whether the band represents a row.
Public propertyReadOnlyGets or sets a value indicating whether the user can edit the band's cells.
Public propertyResizableGets or sets a value indicating whether the band can be resized in the user interface (UI).
Public propertySelectedGets or sets a value indicating whether the band is in a selected user interface (UI) state.
Public propertyStateGets the user interface (UI) state of the element. (Inherited from DataGridViewElement.)
Public propertyTagGets or sets the object that contains data to associate with the band.
Public propertyVisibleGets or sets a value indicating whether the band is visible to the user.
Top

  NameDescription
Public methodCloneCreates an exact copy of this band.
Public methodDispose()Releases all resources used by the DataGridViewBand.
Protected methodDispose(Boolean)Releases the unmanaged resources used by the DataGridViewBand and optionally releases the managed resources.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeReleases the resources associated with the band. (Overrides Object::Finalize().)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnDataGridViewChangedCalled when the band is associated with a different DataGridView. (Overrides DataGridViewElement::OnDataGridViewChanged().)
Protected methodRaiseCellClickRaises the DataGridView::CellClick event. (Inherited from DataGridViewElement.)
Protected methodRaiseCellContentClickRaises the DataGridView::CellContentClick event. (Inherited from DataGridViewElement.)
Protected methodRaiseCellContentDoubleClickRaises the DataGridView::CellContentDoubleClick event. (Inherited from DataGridViewElement.)
Protected methodRaiseCellValueChangedRaises the DataGridView::CellValueChanged event. (Inherited from DataGridViewElement.)
Protected methodRaiseDataErrorRaises the DataGridView::DataError event. (Inherited from DataGridViewElement.)
Protected methodRaiseMouseWheelRaises the Control::MouseWheel event. (Inherited from DataGridViewElement.)
Public methodToStringReturns a string that represents the current band. (Overrides Object::ToString().)
Top

The DataGridViewBand class represents DataGridViewCell elements joined together in a band that can be manipulated as a group. Each element is typically an instance of DataGridViewCell or one of its derived classes. Although a DataGridViewHeaderCell is associated with each band, header cells are not considered elements of the band.

DataGridViewBand is the base class of the DataGridViewRow and DataGridViewColumn classes. There are no public constructors for the DataGridViewBand type, so the only way to access a band is through columns and rows in a DataGridView.

The following code example uses a DataGridViewBand to manipulate properties of groups of cells in a DataGridView.

#using <System.Drawing.dll>
#using <System.dll>
#using <system.windows.forms.dll>
#using <system.drawing.dll>

using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System;
using namespace System::Collections;
public ref class DataGridViewBandDemo: public Form
{
private:

#pragma region S " form setup "  

public:
   DataGridViewBandDemo()
   {
      Button1 = gcnew Button;
      Button2 = gcnew Button;
      Button3 = gcnew Button;
      Button4 = gcnew Button;
      Button5 = gcnew Button;
      Button6 = gcnew Button;
      Button7 = gcnew Button;
      Button8 = gcnew Button;
      Button9 = gcnew Button;
      Button10 = gcnew Button;
      FlowLayoutPanel1 = gcnew FlowLayoutPanel;
      InitializeComponent();
      thirdColumnHeader = L"Main Ingredients";
      boringMeatloaf = L"ground beef";
      boringMeatloafRanking = L"*";
      AddButton( Button1, L"Reset", gcnew EventHandler( this, &DataGridViewBandDemo::Button1_Click ) );
      AddButton( Button2, L"Change Column 3 Header", gcnew EventHandler( this, &DataGridViewBandDemo::Button2_Click ) );
      AddButton( Button3, L"Change Meatloaf Recipe", gcnew EventHandler( this, &DataGridViewBandDemo::Button3_Click ) );
      AddAdditionalButtons();
      InitializeDataGridView();
   }

   DataGridView^ dataGridView;
   Button^ Button1;
   Button^ Button2;
   Button^ Button3;
   Button^ Button4;
   Button^ Button5;
   Button^ Button6;
   Button^ Button7;
   Button^ Button8;
   Button^ Button9;
   Button^ Button10;
   FlowLayoutPanel^ FlowLayoutPanel1;

private:
   void InitializeComponent()
   {
      FlowLayoutPanel1->Location = Point(454,0);
      FlowLayoutPanel1->AutoSize = true;
      FlowLayoutPanel1->FlowDirection = FlowDirection::TopDown;
      AutoSize = true;
      ClientSize = System::Drawing::Size( 614, 360 );
      FlowLayoutPanel1->Name = L"flowlayoutpanel";
      Controls->Add( this->FlowLayoutPanel1 );
      Text = this->GetType()->Name;
   }


#pragma endregion 
#pragma region S " setup DataGridView " 
   String^ thirdColumnHeader;
   String^ boringMeatloaf;
   String^ boringMeatloafRanking;
   bool boringRecipe;
   Boolean shortMode;
   void InitializeDataGridView()
   {
      dataGridView = gcnew System::Windows::Forms::DataGridView;
      Controls->Add( dataGridView );
      dataGridView->Size = System::Drawing::Size( 300, 200 );

      // Create an unbound DataGridView by declaring a 
      // column count.
      dataGridView->ColumnCount = 4;
      AdjustDataGridViewSizing();

      // Set the column header style.
      DataGridViewCellStyle^ columnHeaderStyle = gcnew DataGridViewCellStyle;
      columnHeaderStyle->BackColor = Color::Aqua;
      columnHeaderStyle->Font = gcnew System::Drawing::Font( L"Verdana",10,FontStyle::Bold );
      dataGridView->ColumnHeadersDefaultCellStyle = columnHeaderStyle;

      // Set the column header names.
      dataGridView->Columns[ 0 ]->Name = L"Recipe";
      dataGridView->Columns[ 1 ]->Name = L"Category";
      dataGridView->Columns[ 2 ]->Name = thirdColumnHeader;
      dataGridView->Columns[ 3 ]->Name = L"Rating";

      // Populate the rows. 
      array<String^>^row1 = gcnew array<String^>{
         L"Meatloaf",L"Main Dish",boringMeatloaf,boringMeatloafRanking
      };
      array<String^>^row2 = gcnew array<String^>{
         L"Key Lime Pie",L"Dessert",L"lime juice, evaporated milk",L"****"
      };
      array<String^>^row3 = gcnew array<String^>{
         L"Orange-Salsa Pork Chops",L"Main Dish",L"pork chops, salsa, orange juice",L"****"
      };
      array<String^>^row4 = gcnew array<String^>{
         L"Black Bean and Rice Salad",L"Salad",L"black beans, brown rice",L"****"
      };
      array<String^>^row5 = gcnew array<String^>{
         L"Chocolate Cheesecake",L"Dessert",L"cream cheese",L"***"
      };
      array<String^>^row6 = gcnew array<String^>{
         L"Black Bean Dip",L"Appetizer",L"black beans, sour cream",L"***"
      };
      array<Object^>^rows = gcnew array<Object^>{
         row1,row2,row3,row4,row5,row6
      };
      System::Collections::IEnumerator^ myEnum = rows->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         array<String^>^rowArray = safe_cast<array<String^>^>(myEnum->Current);
         dataGridView->Rows->Add( rowArray );
      }

      PostRowCreation();
      shortMode = false;
      boringRecipe = true;
   }

   void AddButton( Button^ button, String^ buttonLabel, EventHandler^ handler )
   {
      FlowLayoutPanel1->Controls->Add( button );
      button->TabIndex = FlowLayoutPanel1->Controls->Count;
      button->Text = buttonLabel;
      button->AutoSize = true;
      button->Click += handler;
   }


   // Reset columns to initial disorderly arrangement. 
   void Button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Controls->Remove( dataGridView );
      dataGridView->~DataGridView();
      InitializeDataGridView();
   }


   // Change the header in column three. 
   void Button2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Toggle(  &shortMode );
      if ( shortMode )
      {
         dataGridView->Columns[ 2 ]->HeaderText = L"S";
      }
      else
      {
         dataGridView->Columns[ 2 ]->HeaderText = thirdColumnHeader;
      }
   }

   void Toggle( interior_ptr<Boolean> toggleThis )
   {
       *toggleThis =  ! *toggleThis;
   }


   // Change the meatloaf recipe. 
   void Button3_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Toggle(  &boringRecipe );
      if ( boringRecipe )
      {
         SetMeatloaf( boringMeatloaf, boringMeatloafRanking );
      }
      else
      {
         String^ greatMeatloafRecipe = L"1 lb. lean ground beef, "
         L"1/2 cup bread crumbs, 1/4 cup ketchup,"
         L"1/3 tsp onion powder, "
         L"1 clove of garlic, 1/2 pack onion soup mix "
         L" dash of your favorite BBQ Sauce";
         SetMeatloaf( greatMeatloafRecipe, L"***" );
      }
   }

   void SetMeatloaf( String^ recipe, String^ rating )
   {
      dataGridView->Rows[ 0 ]->Cells[ 2 ]->Value = recipe;
      dataGridView->Rows[ 0 ]->Cells[ 3 ]->Value = rating;
   }


#pragma endregion 
#pragma region S " demonstration code "  
   void AddAdditionalButtons()
   {
      AddButton( Button4, L"Freeze First Row", gcnew EventHandler( this, &DataGridViewBandDemo::Button4_Click ) );
      AddButton( Button5, L"Freeze Second Column", gcnew EventHandler( this, &DataGridViewBandDemo::Button5_Click ) );
      AddButton( Button6, L"Hide Salad Row", gcnew EventHandler( this, &DataGridViewBandDemo::Button6_Click ) );
      AddButton( Button7, L"Disable First Column Resizing", gcnew EventHandler( this, &DataGridViewBandDemo::Button7_Click ) );
      AddButton( Button8, L"Make ReadOnly", gcnew EventHandler( this, &DataGridViewBandDemo::Button8_Click ) );
      AddButton( Button9, L"Style Using Tag", gcnew EventHandler( this, &DataGridViewBandDemo::Button9_Click ) );
   }

   void AdjustDataGridViewSizing()
   {
      dataGridView->AutoSizeRowsMode = DataGridViewAutoSizeRowsMode::AllCells;
      dataGridView->ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode::AutoSize;
   }


   // Freeze the first row. 
   void Button4_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      FreezeBand( dataGridView->Rows[ 0 ] );
   }

   void Button5_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      FreezeBand( dataGridView->Columns[ 1 ] );
   }

   void FreezeBand( DataGridViewBand^ band )
   {
      band->Frozen = true;
      DataGridViewCellStyle^ style = gcnew DataGridViewCellStyle;
      style->BackColor = Color::WhiteSmoke;
      band->DefaultCellStyle = style;
   }


   // Hide a band of cells. 
   void Button6_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      DataGridViewBand^ band = dataGridView->Rows[ 3 ];
      band->Visible = false;
   }


   // Turn off user's ability to resize a column. 
   void Button7_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      DataGridViewBand^ band = dataGridView->Columns[ 0 ];
      band->Resizable = DataGridViewTriState::False;
   }


   // Make the the entire DataGridView read only. 
   void Button8_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      System::Collections::IEnumerator^ myEnum = dataGridView->Columns->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         DataGridViewBand^ band = safe_cast<DataGridViewBand^>(myEnum->Current);
         band->ReadOnly = true;
      }
   }


   void PostRowCreation()
   {
      SetBandColor( dataGridView->Columns[ 0 ], Color::CadetBlue );
      SetBandColor( dataGridView->Rows[ 1 ], Color::Coral );
      SetBandColor( dataGridView->Columns[ 2 ], Color::DodgerBlue );
   }

   void SetBandColor( DataGridViewBand^ band, Color color )
   {
      band->Tag = color;
   }


   // Color the bands by the value stored in their tag. 
   void Button9_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      IEnumerator^ myEnum1 = dataGridView->Columns->GetEnumerator();
      while ( myEnum1->MoveNext() )
      {
         DataGridViewBand^ band = static_cast<DataGridViewBand^>(myEnum1->Current);
         if ( band->Tag != nullptr )
         {
            band->DefaultCellStyle->BackColor =  *dynamic_cast<Color^>(band->Tag);
         }
      }

      IEnumerator^ myEnum2 = safe_cast<IEnumerable^>(dataGridView->Rows)->GetEnumerator();
      while ( myEnum2->MoveNext() )
      {
         DataGridViewBand^ band = safe_cast<DataGridViewBand^>(myEnum2->Current);
         if ( band->Tag != nullptr )
         {
            band->DefaultCellStyle->BackColor =  *dynamic_cast<Color^>(band->Tag);
         }
      }
   }


#pragma endregion 

public:
   static void Main()
   {
      Application::Run( gcnew DataGridViewBandDemo );
   }
};

int main()
{
   DataGridViewBandDemo::Main();
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft