Udostępnij za pośrednictwem


How to: Manipulate Rows in the Windows Forms DataGridView Control

 

The following code example shows the various ways to manipulate DataGridView rows using properties of the DataGridViewRow class.

Example

using System.Windows.Forms;
using System;
using System.Drawing;

public class DataGridViewRowDemo : Form
{
    #region "form setup"
    public DataGridViewRowDemo()
    {
        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();
    }

    private DataGridView dataGridView;
    private Button Button1 = new Button();
    private Button Button2 = new Button();
    private Button Button3 = new Button();
    private Button Button4 = new Button();
    private Button Button5 = new Button();
    private Button Button6 = new Button();
    private Button Button7 = new Button();
    private Button Button8 = new Button();
    private Button Button9 = new Button();
    private Button Button10 = new Button();
    private 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;
        dataGridView.ColumnHeadersVisible = true;
        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);
        }

        shortMode = false;
        boringRecipe = true;
    }

    private 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 column 3 header.
    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 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, "Set Row Two Minimum Height",
            new EventHandler(Button4_Click));
        AddButton(Button5, "Set Row One Height",
            new EventHandler(Button5_Click));
        AddButton(Button6, "Label Rows",
            new EventHandler(Button6_Click));
        AddButton(Button7, "Turn on Extra Edge",
            new EventHandler(Button7_Click));
        AddButton(Button8, "Give Cheesecake an Excellent Rating",
            new EventHandler(Button8_Click));
    }

    private void AdjustDataGridViewSizing()
    {
        dataGridView.ColumnHeadersHeightSizeMode = 
            DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        dataGridView.Columns[ratingColumn].Width = 50;
    }

    // Set minimum height.
    private void Button4_Click(object sender, System.EventArgs e)
    {

        int secondRow = 1;
        DataGridViewRow row = dataGridView.Rows[secondRow];
        row.MinimumHeight = 40;
    }

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

        DataGridViewRow row = dataGridView.Rows[0];
        row.Height = 15;
    }

    // Set row labels.
    private void Button6_Click(object sender, System.EventArgs e)
    {

        int rowNumber = 1;
        foreach (DataGridViewRow row in dataGridView.Rows)
        {
            if (row.IsNewRow) continue;
            row.HeaderCell.Value = "Row " + rowNumber;
            rowNumber = rowNumber + 1;
        }
        dataGridView.AutoResizeRowHeadersWidth(
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
    }

    // Set a thick horizontal edge.
    private void Button7_Click(object sender,
        System.EventArgs e)
    {
        int secondRow = 1;
        DataGridViewRow row = dataGridView.Rows[secondRow];
        row.DividerHeight = 10;
    }

    // Give cheescake excellent rating.
    private void Button8_Click(object sender,
        System.EventArgs e)
    {
        UpdateStars(dataGridView.Rows[4], "******************");
    }

    int ratingColumn = 3;

    private void UpdateStars(DataGridViewRow row, string stars)
    {

        row.Cells[ratingColumn].Value = stars;

        // Resize the column width to account for the new value.
        row.DataGridView.AutoResizeColumn(ratingColumn, 
            DataGridViewAutoSizeColumnMode.DisplayedCells);
    }
    #endregion

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

Public Class DataGridViewRowDemo
    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
        dataGridView.ColumnHeadersVisible = True
        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

        shortMode = False
        boringRecipe = True
    End Sub

    Private 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 Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click
        Controls.Remove(dataGridview)
        dataGridView.Dispose()
        InitializeDataGridView(Nothing, Nothing)
    End Sub

    ' Change column 3 header.
    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 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, "Set Row Two Minimum Height")
        AddButton(Button5, "Set Row One Height")
        AddButton(Button6, "Label Rows")
        AddButton(Button7, "Turn on Extra Edge")
        AddButton(Button8, "Give Cheesecake an Excellent Rating")
    End Sub

    Private Sub AdjustDataGridViewSizing()
        dataGridView.ColumnHeadersHeightSizeMode = _
            DataGridViewColumnHeadersHeightSizeMode.AutoSize
        dataGridView.Columns(ratingColumn).Width = 50
    End Sub

    ' Set minimum height.
    Private Sub Button4_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button4.Click

        Dim secondRow As Integer = 1
        Dim row As DataGridViewRow = dataGridView.Rows(secondRow)
        row.MinimumHeight = 40
    End Sub

    ' Set height.
    Private Sub Button5_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button5.Click

        Dim row As DataGridViewRow = dataGridView.Rows(0)
        row.Height = 15
    End Sub

    ' Set row labels.
    Private Sub Button6_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button6.Click

        Dim rowNumber As Integer = 1
        For Each row As DataGridViewRow In dataGridView.Rows
            If row.IsNewRow Then Continue For
            row.HeaderCell.Value = "Row " & rowNumber
            rowNumber = rowNumber + 1
        Next
        dataGridView.AutoResizeRowHeadersWidth( _
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
    End Sub

    ' Set a thick horizontal edge.
    Private Sub Button7_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button7.Click

        Dim secondRow As Integer = 1
        Dim row As DataGridViewRow = dataGridView.Rows(secondRow)
        row.DividerHeight = 10

    End Sub

    ' Give cheescake excellent rating.
    Private Sub Button8_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button8.Click

        UpdateStars(dataGridView.Rows(4), "******************")
    End Sub

    Private ratingColumn As Integer = 3

    Private Sub UpdateStars(ByVal row As DataGridViewRow, _
        ByVal stars As String)

        row.Cells(ratingColumn).Value = stars

        ' Resize the column width to account for the new value.
        row.DataGridView.AutoResizeColumn(ratingColumn, _
            DataGridViewAutoSizeColumnMode.DisplayedCells)

    End Sub
#End Region

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

End Class
#using <System.Drawing.dll>
#using <System.dll>
#using <system.windows.forms.dll>

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

#pragma region S " form setup " 

public:
   DataGridViewRowDemo()
   {
      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;
      thirdColumnHeader = L"Main Ingredients";
      boringMeatloaf = L"ground beef";
      boringMeatloafRanking = L"*";
      ratingColumn = 3;
      AddButton( Button1, L"Reset", gcnew EventHandler( this, &DataGridViewRowDemo::Button1_Click ) );
      AddButton( Button2, L"Change Column 3 Header", gcnew EventHandler( this, &DataGridViewRowDemo::Button2_Click ) );
      AddButton( Button3, L"Change Meatloaf Recipe", gcnew EventHandler( this, &DataGridViewRowDemo::Button3_Click ) );
      InitializeComponent();
      InitializeDataGridView();
      AddAdditionalButtons();
   }


private:
   DataGridView^ dataGridView;
   Button^ Button1;
   Button^ Button2;
   Button^ Button3;
   Button^ Button4;
   Button^ Button5;
   Button^ Button6;
   Button^ Button7;
   Button^ Button8;
   Button^ Button9;
   Button^ Button10;
   FlowLayoutPanel^ FlowLayoutPanel1;
   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;
   bool 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;
      dataGridView->ColumnHeadersVisible = true;
      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 );
      }

      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 column 3 header.
   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 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"Set Row Two Minimum Height", gcnew EventHandler( this, &DataGridViewRowDemo::Button4_Click ) );
      AddButton( Button5, L"Set Row One Height", gcnew EventHandler( this, &DataGridViewRowDemo::Button5_Click ) );
      AddButton( Button6, L"Label Rows", gcnew EventHandler( this, &DataGridViewRowDemo::Button6_Click ) );
      AddButton( Button7, L"Turn on Extra Edge", gcnew EventHandler( this, &DataGridViewRowDemo::Button7_Click ) );
      AddButton( Button8, L"Give Cheesecake an Excellent Rating", gcnew EventHandler( this, &DataGridViewRowDemo::Button8_Click ) );
   }

   void AdjustDataGridViewSizing()
   {
      dataGridView->ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode::AutoSize;
      dataGridView->Columns[ ratingColumn ]->Width = 50;
   }


   // Set minimum height.
   void Button4_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      int secondRow = 1;
      DataGridViewRow^ row = dataGridView->Rows[ secondRow ];
      row->MinimumHeight = 40;
   }


   // Set height.
   void Button5_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      DataGridViewRow^ row = dataGridView->Rows[ 0 ];
      row->Height = 15;
   }


   // Set row labels.
   void Button6_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {

      int rowNumber = 1;
      System::Collections::IEnumerator^ myEnum = safe_cast<System::Collections::IEnumerable^>(dataGridView->Rows)->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         DataGridViewRow^ row = safe_cast<DataGridViewRow^>(myEnum->Current);
         if ( row->IsNewRow )
                  continue;
         row->HeaderCell->Value = String::Format( L"Row {0}", rowNumber );

         rowNumber = rowNumber + 1;
      }

      dataGridView->AutoResizeRowHeadersWidth( DataGridViewRowHeadersWidthSizeMode::AutoSizeToAllHeaders );
   }


   // Set a thick horizontal edge.
   void Button7_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      int secondRow = 1;
      int edgeThickness = 3;
      DataGridViewRow^ row = dataGridView->Rows[ secondRow ];
      row->DividerHeight = 10;
   }


   // Give cheescake excellent rating.
   void Button8_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      UpdateStars( dataGridView->Rows[ 4 ], L"******************" );
   }

   int ratingColumn;
   void UpdateStars( DataGridViewRow^ row, String^ stars )
   {
      row->Cells[ ratingColumn ]->Value = stars;
      
      // Resize the column width to account for the new value.
      row->DataGridView->AutoResizeColumn( ratingColumn, DataGridViewAutoSizeColumnMode::DisplayedCells );
   }


#pragma endregion 

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

};

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

Compiling the Code

This example requires:

  • References to the System, System.Drawing, and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

See Also

DataGridView
DataGridViewBand
DataGridViewRow
DataGridViewColumn
Programming with Cells, Rows, and Columns in the Windows Forms DataGridView Control