C++ > CLI:データグリッドビュー

おそらく、GUIをやる最大の目的は、このデータグリッドビューでしょう。
基本プロジェクトにデータグリッドビューを配置します。
DataGridViewタスクが表示されるので「親コンテナーにドッキングする」を選択します。


データグリッドビュー上で右クリックして「列の追加」をします。


次の3つのデータを入力して追加ボタンを押します。
名前:DelBtn
型:DataGridViewButtonColumn
ヘッダーテキスト:削除


また次のデータを入力して追加ボタンを押します。
名前:SoftName
型:DataGridViewTextBoxColumn
ヘッダーテキスト:Name


同様に次のデータを入力して追加ボタンを押します。
名前:check
型:DataGridViewCheckBoxColumn
ヘッダーテキスト:選択


データグリッドビュー上で右クリックして「列の編集」を選択します。


削除ボタン列の Widthプロパティを 40 にします。


Name列の Widthプロパティを 160 にします。


選択チェックボックス列の Widthプロパティを 40 にします。


このような状態になりました。


データグリッドビューをダブルクリックして以下のコードを追加します。

 if(dataGridView1->CurrentCell->Value == L"削除"){
   // 削除ボタン列かどうかを確認
   if (e->ColumnIndex == this->dataGridView1->Columns["DelBtn"]->Index) {
     if (System::Windows::Forms::DialogResult::Yes == MessageBox::Show("削除しますか", "確認", 
    MessageBoxButtons::YesNo,  MessageBoxIcon::Question)) {
    // 削除
    this->dataGridView1->Rows->RemoveAt(e->RowIndex);
    }
   }
     }


コンストラクタの所にも以下のコードを追加します。

   //
   //TODO: ここにコンストラクター コードを追加します
   //
   this->dataGridView1->Rows->Add(L"削除", L"Maya", 1);
   this->dataGridView1->Rows->Add(L"削除", L"3ds Max", nullptr);
   this->dataGridView1->Rows->Add(L"削除", L"SOFTIMAGE", 1);
   this->dataGridView1->Rows->Add(L"削除", L"CINEMA4D", nullptr);


ビルドして実行してみましょう。

MyForm.cpp

#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#include "MyForm.h"

using namespace Project1;

[STAThreadAttribute]
int main(){
 MyForm ^form1 = gcnew MyForm;
 form1->ShowDialog();
 return 0;
}

MyForm.h

#pragma once

namespace Project1 {

 using namespace System;
 using namespace System::ComponentModel;
 using namespace System::Collections;
 using namespace System::Windows::Forms;
 using namespace System::Data;
 using namespace System::Drawing;

 /// <summary>
 /// MyForm の概要
 /// </summary>
 public ref class MyForm : public System::Windows::Forms::Form
 {
 public:
  MyForm(void)
  {
   InitializeComponent();
   //
   //TODO: ここにコンストラクター コードを追加します
   //
   this->dataGridView1->Rows->Add(L"削除", L"Maya", 1);
   this->dataGridView1->Rows->Add(L"削除", L"3ds Max", nullptr);
   this->dataGridView1->Rows->Add(L"削除", L"SOFTIMAGE", 1);
   this->dataGridView1->Rows->Add(L"削除", L"CINEMA4D", nullptr);
  }

 protected:
  /// <summary>
  /// 使用中のリソースをすべてクリーンアップします。
  /// </summary>
  ~MyForm()
  {
   if (components)
   {
    delete components;
   }
  }
 private: System::Windows::Forms::DataGridView^  dataGridView1;
 private: System::Windows::Forms::DataGridViewButtonColumn^  DelBtn;
 private: System::Windows::Forms::DataGridViewTextBoxColumn^  SoftName;
 private: System::Windows::Forms::DataGridViewCheckBoxColumn^  check;

 protected:


 protected:

 private:
  /// <summary>
  /// 必要なデザイナー変数です。
  /// </summary>
  System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  void InitializeComponent(void)
  {
   this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
   this->DelBtn = (gcnew System::Windows::Forms::DataGridViewButtonColumn());
   this->SoftName = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
   this->check = (gcnew System::Windows::Forms::DataGridViewCheckBoxColumn());
   (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->BeginInit();
   this->SuspendLayout();
   //
   // dataGridView1
   //
   this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
   this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(3) {this->DelBtn,
    this->SoftName, this->check});
   this->dataGridView1->Dock = System::Windows::Forms::DockStyle::Fill;
   this->dataGridView1->Location = System::Drawing::Point(0, 0);
   this->dataGridView1->Name = L"dataGridView1";
   this->dataGridView1->RowTemplate->Height = 21;
   this->dataGridView1->Size = System::Drawing::Size(284, 261);
   this->dataGridView1->TabIndex = 0;
   this->dataGridView1->CellContentClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MyForm::dataGridView1_CellContentClick);
   //
   // DelBtn
   //
   this->DelBtn->HeaderText = L"削除";
   this->DelBtn->Name = L"DelBtn";
   this->DelBtn->Width = 40;
   //
   // SoftName
   //
   this->SoftName->HeaderText = L"Name";
   this->SoftName->Name = L"SoftName";
   this->SoftName->Width = 160;
   //
   // check
   //
   this->check->HeaderText = L"選択";
   this->check->Name = L"check";
   this->check->Width = 40;
   //
   // MyForm
   //
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->ClientSize = System::Drawing::Size(284, 261);
   this->Controls->Add(this->dataGridView1);
   this->Name = L"MyForm";
   this->Text = L"Hello C++/CLI World !!";
   (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();
   this->ResumeLayout(false);

  }
#pragma endregion

 private: System::Void dataGridView1_CellContentClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
     if(dataGridView1->CurrentCell->Value == L"削除"){
   // 削除ボタン列かどうかを確認
   if (e->ColumnIndex == this->dataGridView1->Columns["DelBtn"]->Index) {
     if (System::Windows::Forms::DialogResult::Yes == MessageBox::Show("削除しますか", "確認", 
    MessageBoxButtons::YesNo,  MessageBoxIcon::Question)) {
    // 削除
    this->dataGridView1->Rows->RemoveAt(e->RowIndex);
    }
   }
     }
   }
};
}

 

 

 

 

 

 

最終更新:2013年10月14日 12:43