Merhaba arkadaşlar bu makalemizde C++ da dataGridView nesnesinde My Sql veritabanındaki verilerin 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.
İlk önce Manage Nuget Packages browser kısmından libmysql i kurunuz. Daha sonra References kısmına sağ tıklayalım. Add Referencesi seçip tıklayalım. Açılan Add references penceresnde MySql.Data yı seçip, OK butonuna tıklayalım.
Biz bu örneğimizde MySql de dbemployee şemasındaki staff tablosuna bağlanacağız. (Schema:dbemployee)
Şekil 1
MySql veritabanımızı Şekil 2 deki gibi oluşturuyoruz.
Şekil 2
Formumuzu çalıştırdığımızda My Sql Veritabanımızdaki dbemployee tablomuz DataGridView nesnesinde aşağıdaki gibi görülecektir.
Şekil 3
MyForm.h
#pragma once
namespace cppmysqldatagridview {
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());
System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle3 = (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"Tahoma", 12, 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"Tahoma", 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";
dataGridViewCellStyle3->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;
dataGridViewCellStyle3->BackColor = System::Drawing::SystemColors::Control;
dataGridViewCellStyle3->Font = (gcnew System::Drawing::Font(L"Tahoma", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
dataGridViewCellStyle3->ForeColor = System::Drawing::SystemColors::WindowText;
dataGridViewCellStyle3->SelectionBackColor = System::Drawing::SystemColors::Highlight;
dataGridViewCellStyle3->SelectionForeColor = System::Drawing::SystemColors::HighlightText;
dataGridViewCellStyle3->WrapMode = System::Windows::Forms::DataGridViewTriState::True;
this->dataGridView1->RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
this->dataGridView1->Size = System::Drawing::Size(551, 429);
this->dataGridView1->TabIndex = 0;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(552, 429);
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=1234";
MySqlConnection^ con = gcnew MySqlConnection(str);
MySqlCommand^ cmd = gcnew MySqlCommand("Select * From dbemployee.person; ", con);
MySqlDataReader^ rd;
try
{
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);
}
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cppmysqldatagridview::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