Merhaba arkadaslar bu makalemizde C++ da dataGridView nesnesinde MySql veritabanindaki verilerin gösterimini saglayacagiz. dataGridView nesnesine CheckBox ekliyoruz. Seçili CheckBox satir bilgilerini Messagebox ta gösteriyoruz.
C++ da Windows Form nasil ekleriz? Konusunu daha önceki makalede anlatmistim. Önceki makaleye ulasmak için Buraya tiklayabilirsiniz.
İlk önce Manage Nuget Packages browser kismindan libmysql i kurunuz. Daha sonra References kismina sag tiklayalim. Add Referencesi seçip tiklayalim. Açilan Add references penceresnde MySql.Data yi seçip, OK butonuna tiklayalim.
Biz bu örnegimizde MySql de dbemployee semasindaki staff tablosuna baglanacagiz. (Schema:dbemployee)
Formumuzu çalistirdigimizda MySql Veritabanimizdaki dbemployee tablomuz DataGridView nesnesinde asagidaki gibi görülecektir.
Sekil 1
MyForm.h
#pragma once
namespace cppdatagridviewcheckboxcolumn {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace MySql::Data::MySqlClient;
/// <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
//
dataGridViewCellStyle1->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;
dataGridViewCellStyle1->BackColor = System::Drawing::SystemColors::Control;
dataGridViewCellStyle1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 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(1294, 606);
this->dataGridView1->TabIndex = 0;
this->dataGridView1->CellMouseClick += gcnew System::Windows::Forms::DataGridViewCellMouseEventHandler(this, &MyForm::dataGridView1_CellMouseClick);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(1289, 602);
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) {
String^ str = L"datasource=localhost;port=3306;username=root;password=2344";
MySqlConnection^ con = gcnew MySqlConnection(str);
MySqlCommand^ cmd = gcnew MySqlCommand("Select * From dbemployee.staff; ", con);
try
{
// Add checkbox column.. checkbox sutun ekliyoruz
DataGridViewCheckBoxColumn^ cboxColumn = gcnew DataGridViewCheckBoxColumn;
cboxColumn->Name = "Select";
cboxColumn->HeaderCell->Style->Alignment = DataGridViewContentAlignment::MiddleCenter;
cboxColumn->Width = 40;
dataGridView1->Columns->Add(cboxColumn);
MySqlDataAdapter^ da = gcnew MySqlDataAdapter();
da->SelectCommand = cmd;
DataTable^ dt = gcnew DataTable();
da->Fill(dt);
BindingSource^ bs = gcnew BindingSource();
bs->DataSource = dt;
dataGridView1->DataSource = bs;
}
catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
}
}
private: System::Void dataGridView1_CellMouseClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellMouseEventArgs^ e) {
for each (DataGridViewRow ^ row in dataGridView1->Rows)
{
if (Convert::ToBoolean(row->Cells[0]->Value) == true) {
MessageBox::Show(Convert::ToString(row->Index + 1) + "-" + row->Cells[2]->Value
+ "," + row->Cells[3]->Value + "," + row->Cells[4]->Value
+ "," + row->Cells[5]->Value + "," + row->Cells[9]->Value
+ "," + row->Cells[10]->Value + "," + row->Cells[11]->Value
, "Information", MessageBoxButtons::OK
, MessageBoxIcon::Information
);
}
}
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cppdatagridviewcheckboxcolumn::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüsmek üzere. Bahadir SAHİN
0 comments:
Yorum Gönder