Merhaba arkadaşlar bu makalemizde C++ da dataGridView nesnesine TextBox, Button, Combobox ve Image sutunları ekliyeceğiz. Sonrasında Checkbox ta seçtiğimiz satır bilgilerini mesaj olarak ekranda göstereceğiz.
C++ da Windows Form nasıl ekleriz? Konusunu daha önceki makalede anlatmıştım. Önceki makaleye ulaşmak için Buraya tıklayabilirsiniz.
Şekil 1
MyForm.h
#pragma once
namespace cppdatagridviewbtncboxwithoutdb {
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;
private: System::Windows::Forms::ImageList^ ımageList1;
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#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)
{
this->components = (gcnew System::ComponentModel::Container());
System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle1 = (gcnew System::Windows::Forms::DataGridViewCellStyle());
System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle2 = (gcnew System::Windows::Forms::DataGridViewCellStyle());
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
this->ımageList1 = (gcnew System::Windows::Forms::ImageList(this->components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->BeginInit();
this->SuspendLayout();
//
// dataGridView1
//
dataGridViewCellStyle1->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;
dataGridViewCellStyle1->BackColor = System::Drawing::SystemColors::Control;
dataGridViewCellStyle1->Font = (gcnew System::Drawing::Font(L"Arial", 12, System::Drawing::FontStyle::Bold, 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(966, 577);
this->dataGridView1->TabIndex = 0;
this->dataGridView1->CellClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MyForm::dataGridView1_CellClick);
//
// ımageList1
//
this->ımageList1->ImageStream = (cli::safe_cast<System::Windows::Forms::ImageListStreamer^>(resources->GetObject(L"ımageList1.ImageStream")));
this->ımageList1->TransparentColor = System::Drawing::Color::Transparent;
this->ımageList1->Images->SetKeyName(0, L"thebeautyofawoman.jpg");
this->ımageList1->Images->SetKeyName(1, L"butterfly.jpg");
this->ımageList1->Images->SetKeyName(2, L"sidney.jpg");
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(964, 581);
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::Object^ sender, System::EventArgs^ e) {
dataGridView1->AutoGenerateColumns = false;
dataGridView1->ColumnCount = 5;
//Clear the grid if necessary.. gerekirse grid temizlenir
dataGridView1->DataSource = nullptr;
dataGridView1->Columns->Clear();
//Add Checkbox Column.. checkbox sutunu ekliyoruz
DataGridViewCheckBoxColumn^ cboxColumn = gcnew DataGridViewCheckBoxColumn;
cboxColumn->Name = "Select";
cboxColumn->HeaderCell->Style->Alignment = DataGridViewContentAlignment::MiddleCenter;
//cboxColumn->Width = 40;
dataGridView1->Columns->Add(cboxColumn);
//Add Textbox Column.. textbox sutunu ekliyoruz
DataGridViewTextBoxColumn^ DataGridColumnString1 = gcnew DataGridViewTextBoxColumn();
DataGridColumnString1->HeaderText = "Text Column";
//DataGridColumnString1->ReadOnly = true;
//DataGridColumnName->SortMode = DataGridViewColumnSortMode::NotSortable;
dataGridView1->Columns->Add(DataGridColumnString1);
//Add Button Column.. buton sutunu ekliyoruz
DataGridViewButtonColumn^ DataGridColumnButton1 = gcnew DataGridViewButtonColumn();
DataGridColumnButton1->HeaderText = "Button Column";
//DataGridColumnButton1->ReadOnly = true;
dataGridView1->Columns->Add(DataGridColumnButton1);
//Add Combobox Column.. combobox sutunu ekliyoruz
DataGridViewComboBoxColumn^ DataGridColumnComboBox1 = gcnew DataGridViewComboBoxColumn();
DataGridColumnComboBox1->HeaderText = "ComboBox Column";
//DataGridColumnComboBox1->ReadOnly = true;
//DataGridColumnComboBox1->FlatStyle = FlatStyle::Flat; //Enable to remove button look of cell
DataGridColumnComboBox1->Items->Add("Item 0");
DataGridColumnComboBox1->Items->Add("Item 1");
DataGridColumnComboBox1->Items->Add("Item 2");
dataGridView1->Columns->Add(DataGridColumnComboBox1);
//Add Image Column.. image sutunu ekliyoruz
DataGridViewImageColumn^ DataGridColumnImage1 = gcnew DataGridViewImageColumn();
DataGridColumnImage1->HeaderText = "Image Column";
//DataGridColumnImage1->ReadOnly = true;
dataGridView1->Columns->Add(DataGridColumnImage1);
dataGridView1->Columns[0]->Width = 80;
dataGridView1->Columns[1]->Width = 160;
dataGridView1->Columns[2]->Width = 160;
dataGridView1->Columns[3]->Width = 200;
//dataGridView1->Columns[3]->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::Fill;
dataGridView1->Columns[4]->Width = 200;
//Add the rows.. satirlari ekliyoruz
//Row 0.. 0. satir ekleniyor
dataGridView1->Rows->Add();
dataGridView1->Rows[0]->Height = 30;
dataGridView1->Rows[0]->Cells[0]->Value = "True";
dataGridView1->Rows[0]->Cells[1]->Value = "Row 0";
dataGridView1->Rows[0]->Cells[2]->Value = "Button 0";
dataGridView1->Rows[0]->Cells[3]->Value = "Item 0";
dataGridView1->Rows[0]->Cells[4]->Value = ımageList1->Images[0];
//Row 1.. 1. satir ekleniyor
dataGridView1->Rows->Add();
dataGridView1->Rows[1]->Height = 30;
dataGridView1->Rows[1]->Cells[0]->Value = "False";
dataGridView1->Rows[1]->Cells[1]->Value = "Row 1";
dataGridView1->Rows[1]->Cells[2]->Value = "Button 1";
dataGridView1->Rows[1]->Cells[3]->Value = "Item 1";
dataGridView1->Rows[1]->Cells[4]->Value = ımageList1->Images[1];
//Array Method Row 2.. array metodu ile 2. sutun ekleniyor
array<String^>^ dgRow = gcnew array<String^> { "False", "Row 2", "Button 2", "Item 2" };
dataGridView1->Rows->Add(dgRow);
dataGridView1->Rows[2]->Cells[4]->Value = ımageList1->Images[2];
dataGridView1->Rows[2]->Height = 30;
}
private: System::Void dataGridView1_CellClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
//Ignore clicks of the column headers.. sutun basligina tiklanirsa dikkate alma
if (e->RowIndex > -1)
{
if (e->ColumnIndex == 0)
{
MessageBox::Show(" Row " + e->RowIndex + ", Column " + e->ColumnIndex + " checkbox selected");
}
if (e->ColumnIndex == 2)
{
MessageBox::Show(" Row " + e->RowIndex + ", Column " + e->ColumnIndex + " button clicked");
}
}
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cppdatagridviewbtncboxwithoutdb::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder