Yaz  Font K   lt Yaz  Font B y lt

C++ dataGridView da ComBoBox,CheckBox Sütunlarını Gösterimini Sağlamak

 

Merhaba arkadaşlar bu makalemizde C++ da dataGridView nesnesinin sütunlarında comboBox, checkBox gibi nesneleri gösterimini sağlayacağız. 

C++ da Windows Form nasıl ekleriz? Konusunu daha önceki makalede anlatmıştım. Önceki makaleye ulaşmak için Buraya tıklayabilirsiniz. 

 

Resim1

Şekil 1


 

MyForm.h

 

#pragma once

 

namespace cppdatagridviewwithoutdb {

 

 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>

 /// Summary for MyForm

 /// </summary>

 public ref class MyForm : public System::Windows::Forms::Form

     {

 public:

 MyForm(void)

          {

                InitializeComponent();

 //

 //TODO: Add the constructor code here

 //

          }

 

 protected:

 /// <summary>

 /// Clean up any resources being used.

 /// </summary>

          ~MyForm()

          {

 if (components)

                {

 delete components;

                }

          }

 private: System::Windows::Forms::DataGridView^ dataGridView1;

 protected:

 

 private:

 /// <summary>

 /// Required designer variable.

 /// </summary>

 System::ComponentModel::Container ^components;

 

#pragma region Windows Form Designer generated code

 /// <summary>

 /// Required method for Designer support - do not modify

 /// the contents of this method with the code editor.

 /// </summary>

 void InitializeComponent(void)

          {

 System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle1 = (gcnew System::Windows::Forms::DataGridViewCellStyle());

 System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle2 = (gcnew System::Windows::Forms::DataGridViewCellStyle());

 this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());

 (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->BeginInit();

 this->SuspendLayout();

 // 

 // dataGridView1

 // 

 

 dataGridView1 = gcnew DataGridView;

 dataGridView1->Location = Point(10, 10);

 dataGridView1->Size = System::Drawing::Size(750, 400);

 //add columns. sutunlari ekliyoruz.

 DataGridViewTextBoxColumn^ colFirstName = gcnew DataGridViewTextBoxColumn;

 colFirstName->Name = "FirstName";

 colFirstName->HeaderText = "First Name";

                dataGridView1->Columns->Add(colFirstName);

 

 DataGridViewTextBoxColumn^ colLastName = gcnew DataGridViewTextBoxColumn;

 colLastName->Name = "LastName";

 colLastName->HeaderText = "Last Name";

                dataGridView1->Columns->Add(colLastName);

 

 DataGridViewComboBoxColumn^ colGender = gcnew DataGridViewComboBoxColumn;

 colGender->Name = "Gender";

 colGender->Items->Add("Male");

 colGender->Items->Add("Female");

 colGender->Items->Add("Unknown");

                dataGridView1->Columns->Add(colGender);

 

 DataGridViewCheckBoxColumn^ colFullTime = gcnew DataGridViewCheckBoxColumn;

 colFullTime->Name = "FullTime";

 colFullTime->HeaderText = "Full Time?";

                colFullTime->Width = 70;

                dataGridView1->Columns->Add(colFullTime);

 

 DataGridViewLinkColumn^ colEmailAddress = gcnew DataGridViewLinkColumn();

 colEmailAddress->HeaderText = "Email Address";

                colEmailAddress->Width = 120;

                dataGridView1->Columns->Add(colEmailAddress);

 //dbgrid e 3 satir ekliyoruz.

                dataGridView1->Rows->Add(3);

 //add rows. satir ekliyoruz.

 dataGridView1->Rows[0]->Cells[0]->Value = "Bahadir";

 dataGridView1->Rows[0]->Cells[1]->Value = "Sahin";

 dataGridView1->Rows[0]->Cells[2]->Value = "Male";

 dataGridView1->Rows[0]->Cells[3]->Value = true;

 dataGridView1->Rows[0]->Cells[4]->Value = "basahin@hotmail.com";

 

 dataGridView1->Rows[1]->Cells[0]->Value = "Melissa";

 dataGridView1->Rows[1]->Cells[1]->Value = "Parker";

 dataGridView1->Rows[1]->Cells[2]->Value = "Female";

 dataGridView1->Rows[1]->Cells[3]->Value = true;

 dataGridView1->Rows[1]->Cells[4]->Value = "melparker@gmail.com";

 

 dataGridView1->Rows[2]->Cells[0]->Value = "Tommy";

 dataGridView1->Rows[2]->Cells[1]->Value = "Paton";

 dataGridView1->Rows[2]->Cells[2]->Value = "Male";

 dataGridView1->Rows[2]->Cells[3]->Value = false;

 dataGridView1->Rows[2]->Cells[4]->Value = "tommypat@hotmail.com";

 

                Controls->Add(dataGridView1);

 Text = L"Exercise";

 StartPosition = FormStartPosition::CenterScreen;

 

 

 dataGridViewCellStyle1->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;

 dataGridViewCellStyle1->BackColor = System::Drawing::SystemColors::Control;

 dataGridViewCellStyle1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

 static_cast<System::Byte>(162)));

 dataGridViewCellStyle1->ForeColor = System::Drawing::SystemColors::WindowText;

 dataGridViewCellStyle1->SelectionBackColor = System::Drawing::SystemColors::Highlight;

 dataGridViewCellStyle1->SelectionForeColor = System::Drawing::SystemColors::HighlightText;

 dataGridViewCellStyle1->WrapMode = System::Windows::Forms::DataGridViewTriState::True;

 this->dataGridView1->ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;

 this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;

 dataGridViewCellStyle2->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;

 dataGridViewCellStyle2->BackColor = System::Drawing::SystemColors::Window;

 dataGridViewCellStyle2->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

 static_cast<System::Byte>(162)));

 dataGridViewCellStyle2->ForeColor = System::Drawing::SystemColors::ControlText;

 dataGridViewCellStyle2->SelectionBackColor = System::Drawing::SystemColors::Highlight;

 dataGridViewCellStyle2->SelectionForeColor = System::Drawing::SystemColors::HighlightText;

 dataGridViewCellStyle2->WrapMode = System::Windows::Forms::DataGridViewTriState::False;

 this->dataGridView1->DefaultCellStyle = dataGridViewCellStyle2;

 this->dataGridView1->Location = System::Drawing::Point(0, 0);

 this->dataGridView1->Name = L"dataGridView1";

 this->dataGridView1->Size = System::Drawing::Size(761, 411);

 this->dataGridView1->TabIndex = 0;

 // 

 // MyForm

 // 

 this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

 this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

 this->ClientSize = System::Drawing::Size(761, 407);

 this->Controls->Add(this->dataGridView1);

 this->Name = L"MyForm";

 this->Text = L"MyForm";

 this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);

 (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->EndInit();

 this->ResumeLayout(false);

 

          }

#pragma endregion

 private: System::Void MyForm_Load(System::Objectsender, System::EventArgse

     {

 this->Text = L"dataGridView example...";

     }

     };

}

 

MyForm.cpp

 

#include "MyForm.h"

using namespace System;

using namespace System::Windows::Forms;

[STAThreadAttribute]

void Main(array<String^>^ args) {

 Application::EnableVisualStyles();

 Application::SetCompatibleTextRenderingDefault(false);

 cppdbgridviewwithoutdb::MyForm form;

 Application::Run(% form);

}

      

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN