DataGridView.ColumnHeadersVisible プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataGridView.ColumnHeadersVisible プロパティの意味・解説 

DataGridView.ColumnHeadersVisible プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

ヘッダー行が表示されるかどうかを示す値を取得または設定します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Property ColumnHeadersVisible As
 Boolean
Dim instance As DataGridView
Dim value As Boolean

value = instance.ColumnHeadersVisible

instance.ColumnHeadersVisible = value
public bool ColumnHeadersVisible { get;
 set; }
public:
property bool ColumnHeadersVisible {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_ColumnHeadersVisible ()

/** @property */
public void set_ColumnHeadersVisible (boolean
 value)
public function get ColumnHeadersVisible
 () : boolean

public function set ColumnHeadersVisible
 (value : boolean)

プロパティ
ヘッダー表示する場合trueそれ以外場合false既定値true です。

例外例外
例外種類条件

InvalidOperationException

このプロパティ設定時に指定された値は false であり、1 つ上の列に ColumnHeader の InheritedAutoSizeMode プロパティ値が設定されています。

解説解説
使用例使用例

バインドされていない DataGridView コントロール作成して、列ヘッダー表示されるように ColumnHeadersVisible プロパティtrue設定するコード例次に示します

Private Sub InitializeDataGridView()

    ' Create an unbound DataGridView by declaring a column count.
    dataGridView1.ColumnCount = 4
    dataGridView1.ColumnHeadersVisible = True

    ' Set the column header style.
    Dim columnHeaderStyle As New
 DataGridViewCellStyle()

    columnHeaderStyle.BackColor = Color.Beige
    columnHeaderStyle.Font = New Font("Verdana",
 10, FontStyle.Bold)
    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle

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

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

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

End Sub

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

    ' Resize the height of the column headers. 
    dataGridView1.AutoResizeColumnHeadersHeight()

    ' Resize all the row heights to fit the contents of all 
    ' non-header cells.
    dataGridView1.AutoResizeRows( _
        DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)

End Sub

Private Sub InitializeContextMenu()

    ' Create the menu item.
    Dim getRecipe As New
 ToolStripMenuItem( _
        "Search for recipe", Nothing,
 AddressOf ShortcutMenuClick)

    ' Add the menu item to the shortcut menu.
    Dim recipeMenu As New
 ContextMenuStrip()
    recipeMenu.Items.Add(getRecipe)

    ' Set the shortcut menu for the first column.
    dataGridView1.Columns(0).ContextMenuStrip = recipeMenu

End Sub

Private clickedCell As DataGridViewCell

Private Sub dataGridView1_MouseDown(ByVal
 sender As Object, _
    ByVal e As MouseEventArgs) Handles
 dataGridView1.MouseDown

    ' If the user right-clicks a cell, store it for use by the 
    ' shortcut menu.
    If e.Button = MouseButtons.Right Then
        Dim hit As DataGridView.HitTestInfo
 = _
            dataGridView1.HitTest(e.X, e.Y)
        If hit.Type = DataGridViewHitTestType.Cell Then
            clickedCell = _
                dataGridView1.Rows(hit.RowIndex).Cells(hit.ColumnIndex)
        End If
    End If

End Sub

Private Sub ShortcutMenuClick(ByVal
 sender As Object, _
    ByVal e As System.EventArgs)

    If Not (clickedCell Is
 Nothing) Then
        'Retrieve the recipe name.
        Dim recipeName As String
 = CStr(clickedCell.Value)

        'Search for the recipe.
        System.Diagnostics.Process.Start( _
            "http://search.msn.com/results.aspx?q="
 + recipeName)
    End If

End Sub
private void InitializeDataGridView()
{
    // Create an unbound DataGridView by declaring a column count.
    dataGridView1.ColumnCount = 4;
    dataGridView1.ColumnHeadersVisible = true;

    // Set the column header style.
    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

    columnHeaderStyle.BackColor = Color.Beige;
    columnHeaderStyle.Font = new Font("Verdana", 10,
 FontStyle.Bold);
    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

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

    // Populate the rows.
    string[] row1 = new string[]
 { "Meatloaf", "Main Dish", "ground beef",
        "**" };
    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)
    {
        dataGridView1.Rows.Add(rowArray);
    }
}

private void button1_Click(object sender, System.EventArgs
 e)
{
    // Resize the height of the column headers. 
    dataGridView1.AutoResizeColumnHeadersHeight();

    // Resize all the row heights to fit the contents of all non-header
 cells.
    dataGridView1.AutoResizeRows(
        DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);
}

private void InitializeContextMenu()
{
    // Create the menu item.
    ToolStripMenuItem getRecipe = new ToolStripMenuItem("Search
 for recipe", null,
        new System.EventHandler(ShortcutMenuClick));

    // Add the menu item to the shortcut menu.
    ContextMenuStrip recipeMenu = new ContextMenuStrip();
    recipeMenu.Items.Add(getRecipe); 

    // Set the shortcut menu for the first column.
    dataGridView1.Columns[0].ContextMenuStrip = recipeMenu;
    dataGridView1.MouseDown += new MouseEventHandler(dataGridView1_MouseDown);
}

private DataGridViewCell clickedCell;

private void dataGridView1_MouseDown(object
 sender, MouseEventArgs e)
{
// If the user right-clicks a cell, store it for use by the shortcut
 menu.
    if (e.Button == MouseButtons.Right)
    {
        DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
        if (hit.Type == DataGridViewHitTestType.Cell)
        {
            clickedCell =
                dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
        }
    }
}

private void ShortcutMenuClick(object sender,
 System.EventArgs e)
{
    if (clickedCell != null)
    {
        //Retrieve the recipe name.
        string recipeName = (string)clickedCell.Value;

        //Search for the recipe.
        System.Diagnostics.Process.Start(
            "http://search.msn.com/results.aspx?q=" + recipeName);
            //null);
    }
}
   void InitializeDataGridView()
   {
      this->Size = System::Drawing::Size( 600, 600 );
      dataGridView1->Size = System::Drawing::Size( 450, 400 );

      // Create an unbound DataGridView by declaring a column count.
      dataGridView1->ColumnCount = 4;
      dataGridView1->ColumnHeadersVisible = true;

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

      // Set the column header names.
      dataGridView1->Columns[ 0 ]->Name = "Recipe";
      dataGridView1->Columns[ 1 ]->Name = "Category";
      dataGridView1->Columns[ 2 ]->Name = "Main Ingredients";
      dataGridView1->Columns[ 3 ]->Name = "Rating";

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

   void Button1_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      // Resize the height of the column headers. 
      dataGridView1->AutoResizeColumnHeadersHeight();

      // Resize all the row heights to fit the contents of all non-header
 cells.
      dataGridView1->AutoResizeRows(
            DataGridViewAutoSizeRowsMode::AllCellsExceptHeaders);
   }

   void InitializeContextMenu()
   {
      // Create the menu item.
      MenuItem^ getRecipe = gcnew MenuItem( "Search for recipe",gcnew
 System::EventHandler( this, &Form1::OnMenuClick ) );

      // Add the menu item to the shortcut menu.
      System::Windows::Forms::ContextMenuStrip^ recipeMenu = gcnew System::Windows::Forms::ContextMenuStrip();

      // Set the shortcut menu for the first column.
      dataGridView1->Columns[ 0 ]->ContextMenuStrip = recipeMenu;
   }

   void OnMenuClick( Object^ /*sender*/, System::EventArgs^ /*e*/
 )
   {
      if ( dataGridView1->CurrentCell != nullptr )
      {
         //Retrieve the recipe name.
         String^ recipeName = dynamic_cast<String^>(dataGridView1->CurrentCell->Value);

         //Search for the recipe.
         System::Diagnostics::Process::Start( String::Format( "http://search.msn.com/results.aspx?q={0}",
 recipeName ), nullptr );
      }
   }

private:

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataGridView.ColumnHeadersVisible プロパティ」の関連用語

DataGridView.ColumnHeadersVisible プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataGridView.ColumnHeadersVisible プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS