다음을 통해 공유


DataGridViewBand 클래스

정의

DataGridView 컨트롤에 있는 요소의 선형 컬렉션을 나타냅니다.

public ref class DataGridViewBand : System::Windows::Forms::DataGridViewElement, ICloneable, IDisposable
public class DataGridViewBand : System.Windows.Forms.DataGridViewElement, ICloneable, IDisposable
type DataGridViewBand = class
    inherit DataGridViewElement
    interface ICloneable
    interface IDisposable
Public Class DataGridViewBand
Inherits DataGridViewElement
Implements ICloneable, IDisposable
상속
DataGridViewBand
파생
구현

예제

다음 코드 예제에서는 를 사용하여 DataGridViewBand 의 셀 그룹 속성을 조작합니다 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 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();
}
using System.Drawing;
using System.Windows.Forms;
using System;

public class DataGridViewBandDemo : Form
{
    #region "form setup"
    public DataGridViewBandDemo()
    {
        InitializeComponent();

        AddButton(Button1, "Reset",
            new EventHandler(Button1_Click));
        AddButton(Button2, "Change Column 3 Header",
            new EventHandler(Button2_Click));
        AddButton(Button3, "Change Meatloaf Recipe",
            new EventHandler(Button3_Click));
        AddAdditionalButtons();

        InitializeDataGridView();
    }

    DataGridView dataGridView;
    Button Button1 = new Button();
    Button Button2 = new Button();
    Button Button3 = new Button();
    Button Button4 = new Button();
    Button Button5 = new Button();
    Button Button6 = new Button();
    Button Button7 = new Button();
    Button Button8 = new Button();
    Button Button9 = new Button();
    Button Button10 = new Button();
    FlowLayoutPanel FlowLayoutPanel1 = new FlowLayoutPanel();

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

    #region "setup DataGridView"

    private string thirdColumnHeader = "Main Ingredients";
    private string boringMeatloaf = "ground beef";
    private string boringMeatloafRanking = "*";
    private bool boringRecipe;
    private bool shortMode;

    private void InitializeDataGridView()
    {
        dataGridView = new System.Windows.Forms.DataGridView();
        Controls.Add(dataGridView);
        dataGridView.Size = new Size(300, 200);

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

        // Set the column header style.
        DataGridViewCellStyle columnHeaderStyle =
            new DataGridViewCellStyle();
        columnHeaderStyle.BackColor = Color.Aqua;
        columnHeaderStyle.Font =
            new Font("Verdana", 10, FontStyle.Bold);
        dataGridView.ColumnHeadersDefaultCellStyle =
            columnHeaderStyle;

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

        // Populate the rows.
        string[] row1 = new string[]{"Meatloaf", 
                                        "Main Dish", boringMeatloaf, boringMeatloafRanking};
        string[] row2 = new string[]{"Key Lime Pie", 
                                        "Dessert", "lime juice, evaporated milk", "****"};
        string[] row3 = new string[]{"Orange-Salsa Pork Chops", 
                                        "Main Dish", "pork chops, salsa, orange juice", "****"};
        string[] row4 = new string[]{"Black Bean and Rice Salad", 
                                        "Salad", "black beans, brown rice", "****"};
        string[] row5 = new string[]{"Chocolate Cheesecake", 
                                        "Dessert", "cream cheese", "***"};
        string[] row6 = new string[]{"Black Bean Dip", "Appetizer",
                                        "black beans, sour cream", "***"};
        object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };

        foreach (string[] rowArray in rows)
        {
            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.
    private void Button1_Click(object sender, System.EventArgs e)
    {
        Controls.Remove(dataGridView);
        dataGridView.Dispose();
        InitializeDataGridView();
    }

    // Change the header in column three.
    private void Button2_Click(object sender,
        System.EventArgs e)
    {
        Toggle(ref shortMode);
        if (shortMode)
        { dataGridView.Columns[2].HeaderText = "S"; }
        else
        { dataGridView.Columns[2].HeaderText = thirdColumnHeader; }
    }

    private static void Toggle(ref bool toggleThis)
    {
        toggleThis = !toggleThis;
    }

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

    private void SetMeatloaf(string recipe, string rating)
    {
        dataGridView.Rows[0].Cells[2].Value = recipe;
        dataGridView.Rows[0].Cells[3].Value = rating;
    }
    #endregion

    #region "demonstration code"
    private void AddAdditionalButtons()
    {
        AddButton(Button4, "Freeze First Row",
            new EventHandler(Button4_Click));
        AddButton(Button5, "Freeze Second Column",
            new EventHandler(Button5_Click));
        AddButton(Button6, "Hide Salad Row",
            new EventHandler(Button6_Click));
        AddButton(Button7, "Disable First Column Resizing",
            new EventHandler(Button7_Click));
        AddButton(Button8, "Make ReadOnly",
            new EventHandler(Button8_Click));
        AddButton(Button9, "Style Using Tag",
            new EventHandler(Button9_Click));
    }

    private void AdjustDataGridViewSizing()
    {
        dataGridView.AutoSizeRowsMode =
            DataGridViewAutoSizeRowsMode.AllCells;
        dataGridView.ColumnHeadersHeightSizeMode = 
            DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    }

    // Freeze the first row.
    private void Button4_Click(object sender, System.EventArgs e)
    {

        FreezeBand(dataGridView.Rows[0]);
    }

    private void Button5_Click(object sender, System.EventArgs e)
    {

        FreezeBand(dataGridView.Columns[1]);
    }

    private static void FreezeBand(DataGridViewBand band)
    {
        band.Frozen = true;
        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.BackColor = Color.WhiteSmoke;
        band.DefaultCellStyle = style;
    }

    // Hide a band of cells.
    private 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.
    private void Button7_Click(object sender, EventArgs e)
    {

        DataGridViewBand band = dataGridView.Columns[0];
        band.Resizable = DataGridViewTriState.False;
    }

    // Make the entire DataGridView read only.
    private void Button8_Click(object sender, System.EventArgs e)
    {
        foreach (DataGridViewBand band in dataGridView.Columns)
        {
            band.ReadOnly = true;
        }
    }

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

    private static void SetBandColor(DataGridViewBand band, Color color)
    {
        band.Tag = color;
    }

    // Color the bands by the value stored in their tag.
    private void Button9_Click(object sender, System.EventArgs e)
    {

        foreach (DataGridViewBand band in dataGridView.Columns)
        {
            if (band.Tag != null)
            {
                band.DefaultCellStyle.BackColor = (Color)band.Tag;
            }
        }

        foreach (DataGridViewBand band in dataGridView.Rows)
        {
            if (band.Tag != null)
            {
                band.DefaultCellStyle.BackColor = (Color)band.Tag;
            }
        }
    }
    #endregion

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new DataGridViewBandDemo());
    }
}
Imports System.Windows.Forms
Imports System.Drawing

Public Class DataGridViewBandDemo
    Inherits Form

#Region "Form setup"
    Public Sub New()
        MyBase.New()
        InitializeComponent()

        AddButton(Button1, "Reset")
        AddButton(Button2, "Change Column 3 Header")
        AddButton(Button3, "Change Meatloaf Recipe")
        AddAdditionalButtons()
    End Sub

    Friend WithEvents dataGridView As DataGridView
    Friend WithEvents Button1 As Button = New Button()
    Friend WithEvents Button2 As Button = New Button()
    Friend WithEvents Button3 As Button = New Button()
    Friend WithEvents Button4 As Button = New Button()
    Friend WithEvents Button5 As Button = New Button()
    Friend WithEvents Button6 As Button = New Button()
    Friend WithEvents Button7 As Button = New Button()
    Friend WithEvents Button8 As Button = New Button()
    Friend WithEvents Button9 As Button = New Button()
    Friend WithEvents Button10 As Button = New Button()
    Friend WithEvents FlowLayoutPanel1 As FlowLayoutPanel _
        = New FlowLayoutPanel()

    Private Sub InitializeComponent()
        FlowLayoutPanel1.Location = New Point(454, 0)
        FlowLayoutPanel1.AutoSize = True
        FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown
        FlowLayoutPanel1.Name = "flowlayoutpanel"
        ClientSize = New System.Drawing.Size(614, 360)
        Controls.Add(FlowLayoutPanel1)
        Text = Me.GetType.Name
        AutoSize = True
    End Sub
#End Region

#Region "setup DataGridView"
    Private thirdColumnHeader As String = "Main Ingredients"
    Private boringMeatloaf As String = "ground beef"
    Private boringMeatloafRanking As String = "*"
    Private boringRecipe As Boolean
    Private shortMode As Boolean

    Private Sub InitializeDataGridView(ByVal ignored As Object, _
    ByVal ignoredToo As EventArgs) Handles Me.Load

        dataGridView = New System.Windows.Forms.DataGridView
        Controls.Add(dataGridView)
        dataGridView.Size = New Size(300, 200)

        ' Create an unbound DataGridView by declaring a
        ' column count.
        dataGridView.ColumnCount = 4
        AdjustDataGridViewSizing()

        ' Set the column header style.
        Dim columnHeaderStyle As New DataGridViewCellStyle
        columnHeaderStyle.BackColor = Color.Aqua
        columnHeaderStyle.Font = _
            New Font("Verdana", 10, FontStyle.Bold)
        dataGridView.ColumnHeadersDefaultCellStyle = _
            columnHeaderStyle

        ' Set the column header names.
        dataGridView.Columns(0).Name = "Recipe"
        dataGridView.Columns(1).Name = "Category"
        dataGridView.Columns(2).Name = thirdColumnHeader
        dataGridView.Columns(3).Name = "Rating"

        ' Populate the rows.
        Dim row1 As String() = New String() _
            {"Meatloaf", "Main Dish", boringMeatloaf, _
            boringMeatloafRanking}
        Dim row2 As String() = New String() _
            {"Key Lime Pie", "Dessert", _
            "lime juice, evaporated milk", _
            "****"}
        Dim row3 As String() = New String() _
            {"Orange-Salsa Pork Chops", "Main Dish", _
            "pork chops, salsa, orange juice", "****"}
        Dim row4 As String() = New String() _
            {"Black Bean and Rice Salad", "Salad", _
            "black beans, brown rice", _
            "****"}
        Dim row5 As String() = New String() _
            {"Chocolate Cheesecake", "Dessert", "cream cheese", _
            "***"}
        Dim row6 As String() = New String() _
            {"Black Bean Dip", "Appetizer", _
            "black beans, sour cream", _
                "***"}
        Dim rows As Object() = New Object() {row1, row2, _
            row3, row4, row5, row6}

        Dim rowArray As String()
        For Each rowArray In rows
            dataGridView.Rows.Add(rowArray)
        Next

        PostRowCreation()

        shortMode = False
        boringRecipe = True
    End Sub

    Protected Sub AddButton(ByVal button As Button, _
        ByVal buttonLabel As String)

        FlowLayoutPanel1.Controls.Add(button)
        button.TabIndex = FlowLayoutPanel1.Controls.Count
        button.Text = buttonLabel
        button.AutoSize = True
    End Sub

    ' Reset columns to initial disorderly arrangement.
    Private Sub ResetToDisorder(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click
        Controls.Remove(dataGridview)
        dataGridView.Dispose()
        InitializeDataGridView(Nothing, Nothing)
    End Sub

    ' Change the header in column three.
    Private Sub Button2_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles Button2.Click

        Toggle(shortMode)
        If shortMode Then dataGridView.Columns(2).HeaderText = _
            "S" _
            Else dataGridView.Columns(2).HeaderText = _
                thirdColumnHeader
    End Sub

    Private Shared Sub Toggle(ByRef toggleThis As Boolean)
        toggleThis = Not toggleThis
    End Sub

    ' Change the meatloaf recipe.
    Private Sub Button3_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles Button3.Click

        Toggle(boringRecipe)
        If boringRecipe Then
            SetMeatloaf(boringMeatloaf, boringMeatloafRanking)
        Else
            Dim greatMeatloafRecipe As String = "1 lb. lean ground beef, " _
                & "1/2 cup bread crumbs, 1/4 cup ketchup," _
                & "1/3 tsp onion powder, " _
                & "1 clove of garlic, 1/2 pack onion soup mix " _
                & " dash of your favorite BBQ Sauce"
            SetMeatloaf(greatMeatloafRecipe, "***")
        End If
    End Sub

    Private Sub SetMeatloaf(ByVal recipe As String, _
        ByVal rating As String)

        dataGridView.Rows(0).Cells(2).Value = recipe
        dataGridView.Rows(0).Cells(3).Value = rating
    End Sub
#End Region

#Region "demonstration code"
    Private Sub AddAdditionalButtons()
        AddButton(Button4, "Freeze First Row")
        AddButton(Button5, "Freeze Second Column")
        AddButton(Button6, "Hide Salad Row")
        AddButton(Button7, "Disable First Column Resizing")
        AddButton(Button8, "Make ReadOnly")
        AddButton(Button9, "Style Using Tag")
    End Sub

    Private Sub AdjustDataGridViewSizing()
        dataGridView.AutoSizeRowsMode = _
            DataGridViewAutoSizeRowsMode.AllCells
        dataGridView.ColumnHeadersHeightSizeMode = _
            DataGridViewColumnHeadersHeightSizeMode.AutoSize
    End Sub

    ' Freeze the first row.
    Private Sub Button4_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button4.Click

        FreezeBand(dataGridView.Rows(0))
    End Sub

    Private Sub FreezeColumn(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button5.Click

        FreezeBand(dataGridView.Columns(1))
    End Sub

    Private Shared Sub FreezeBand(ByVal band As DataGridViewBand)

        band.Frozen = True
        Dim style As DataGridViewCellStyle = New DataGridViewCellStyle()
        style.BackColor = Color.WhiteSmoke
        band.DefaultCellStyle = style

    End Sub

    ' Hide a band of cells.
    Private Sub Button6_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button6.Click

        Dim band As DataGridViewBand = dataGridView.Rows(3)
        band.Visible = False
    End Sub

    ' Turn off user's ability to resize a column.
    Private Sub Button7_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button7.Click

        Dim band As DataGridViewBand = dataGridView.Columns(0)
        band.Resizable = DataGridViewTriState.False
    End Sub

    ' Make the entire DataGridView read only.
    Private Sub Button8_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button8.Click

        For Each band As DataGridViewBand In dataGridView.Columns
            band.ReadOnly = True
        Next
    End Sub

    Private Sub PostRowCreation()
        SetBandColor(dataGridView.Columns(0), Color.CadetBlue)
        SetBandColor(dataGridView.Rows(1), Color.Coral)
        SetBandColor(dataGridView.Columns(2), Color.DodgerBlue)
    End Sub

    Private Shared Sub SetBandColor(ByVal band As DataGridViewBand, _
        ByVal color As Color)
        band.Tag = color
    End Sub

    ' Color the bands by the value stored in their tag.
    Private Sub Button9_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button9.Click

        For Each band As DataGridViewBand In dataGridView.Columns
            If band.Tag IsNot Nothing Then
                band.DefaultCellStyle.BackColor = _
                    CType(band.Tag, Color)
            End If
        Next

        For Each band As DataGridViewBand In dataGridView.Rows
            If band.Tag IsNot Nothing Then
                band.DefaultCellStyle.BackColor = _
                    CType(band.Tag, Color)
            End If
        Next
    End Sub
#End Region

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New DataGridViewBandDemo())
    End Sub
End Class

설명

클래스는 DataGridViewBandDataGridViewCell 그룹으로 조작할 수 있는 밴드에서 함께 조인된 요소를 나타냅니다. 각 요소는 일반적으로 파생 클래스 중 DataGridViewCell 하나 또는 의 instance. DataGridViewHeaderCell 은 각 밴드와 연결되어 있지만 헤더 셀은 대역의 요소로 간주되지 않습니다.

DataGridViewBand는 및 DataGridViewColumn 클래스의 DataGridViewRow 기본 클래스입니다. 형식에 대한 DataGridViewBand 공용 생성자가 없으므로 밴드에 액세스하는 유일한 방법은 의 열과 행을 사용하는 DataGridView것입니다.

속성

ContextMenuStrip

밴드의 바로 가기 메뉴를 가져오거나 설정합니다.

DataGridView

이 요소와 관련된 DataGridView 컨트롤을 가져옵니다.

(다음에서 상속됨 DataGridViewElement)
DefaultCellStyle

밴드의 기본 셀 스타일을 가져오거나 설정합니다.

DefaultHeaderCellType

기본 머리글 셀의 런타임 형식을 가져오거나 설정합니다.

Displayed

밴드가 현재 화면에 표시되는지 여부를 나타내는 값을 가져옵니다.

Frozen

사용자가 DataGridView를 스크롤하면 밴드가 이동할지 여부를 나타내는 값을 가져오거나 설정합니다.

HasDefaultCellStyle

DefaultCellStyle 속성이 설정되었는지 여부를 나타내는 값을 가져옵니다.

HeaderCellCore

DataGridViewBand의 머리글 셀을 가져오거나 설정합니다.

Index

DataGridView 컨트롤에서 밴드의 상대적 위치를 가져옵니다.

InheritedStyle

스타일 상속을 고려하여 현재 밴드에 적용되는 셀 스타일을 가져옵니다.

IsRow

밴드가 행을 나타내는지 여부를 나타내는 값을 가져옵니다.

ReadOnly

사용자가 밴드의 셀을 편집할 수 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

Resizable

UI(사용자 인터페이스)에서 밴드의 크기를 조정할 수 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

Selected

밴드가 선택한 UI(사용자 인터페이스) 상태에 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

State

요소의 UI(사용자 인터페이스) 상태를 가져옵니다.

(다음에서 상속됨 DataGridViewElement)
Tag

밴드에 연결할 데이터가 포함된 개체를 가져오거나 설정합니다.

Visible

사용자에게 밴드를 표시할지 여부를 나타내는 값을 가져오거나 설정합니다.

메서드

Clone()

이 밴드와 정확하게 일치하는 복사본을 만듭니다.

Dispose()

DataGridViewBand에서 사용하는 모든 리소스를 해제합니다.

Dispose(Boolean)

DataGridViewBand에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
Finalize()

밴드와 연결된 리소스를 해제합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
OnDataGridViewChanged()

밴드가 다른 DataGridView에 연결될 때 호출됩니다.

RaiseCellClick(DataGridViewCellEventArgs)

CellClick 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseCellContentClick(DataGridViewCellEventArgs)

CellContentClick 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseCellContentDoubleClick(DataGridViewCellEventArgs)

CellContentDoubleClick 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseCellValueChanged(DataGridViewCellEventArgs)

CellValueChanged 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseDataError(DataGridViewDataErrorEventArgs)

DataError 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseMouseWheel(MouseEventArgs)

MouseWheel 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
ToString()

현재 밴드를 나타내는 문자열을 반환합니다.

적용 대상

추가 정보