#pragma once
namespace CppCLR_WinformsProjekt {
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 System::Data::SqlClient;
/// <summary>
/// Zusammenfassung für Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
SqlConnection^ conn = gcnew SqlConnection("Data Source=.;Initial Catalog=database10;Integrated Security=true");
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Konstruktorcode hier hinzufügen.
//
}
protected:
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
protected:
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::TextBox^ textBox4;
private: System::Windows::Forms::DataGridView^ dataGridView1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Label^ idlbl;
private: System::Windows::Forms::Label^ code;
private: System::Windows::Forms::TextBox^ textBox1;
private:
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->textBox4 = (gcnew System::Windows::Forms::TextBox());
this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->idlbl = (gcnew System::Windows::Forms::Label());
this->code = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->BeginInit();
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(50, 26);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(29, 24);
this->label1->TabIndex = 0;
this->label1->Text = L"ID";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label2->Location = System::Drawing::Point(50, 111);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(102, 24);
this->label2->TabIndex = 1;
this->label2->Text = L"Firstname";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label3->Location = System::Drawing::Point(50, 178);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(99, 24);
this->label3->TabIndex = 2;
this->label3->Text = L"Lastname";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label4->Location = System::Drawing::Point(50, 250);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(52, 24);
this->label4->TabIndex = 3;
this->label4->Text = L"Sum";
//
// textBox2
//
this->textBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox2->Location = System::Drawing::Point(171, 111);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(147, 29);
this->textBox2->TabIndex = 5;
//
// textBox3
//
this->textBox3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox3->Location = System::Drawing::Point(171, 178);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(147, 29);
this->textBox3->TabIndex = 6;
//
// textBox4
//
this->textBox4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox4->Location = System::Drawing::Point(171, 236);
this->textBox4->Name = L"textBox4";
this->textBox4->Size = System::Drawing::Size(147, 29);
this->textBox4->TabIndex = 7;
//
// dataGridView1
//
this->dataGridView1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
this->dataGridView1->AutoSizeColumnsMode = System::Windows::Forms::DataGridViewAutoSizeColumnsMode::Fill;
this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dataGridView1->Location = System::Drawing::Point(324, 53);
this->dataGridView1->Name = L"dataGridView1";
this->dataGridView1->Size = System::Drawing::Size(489, 501);
this->dataGridView1->TabIndex = 8;
this->dataGridView1->CellClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &Form1::dataGridView1_CellClick);
//
// button1
//
this->button1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
this->button1->AutoSize = true;
this->button1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(255)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button1->Location = System::Drawing::Point(13, 576);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(115, 42);
this->button1->TabIndex = 9;
this->button1->Text = L"New";
this->button1->UseVisualStyleBackColor = false;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
this->button2->AutoSize = true;
this->button2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button2->Location = System::Drawing::Point(144, 576);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(115, 42);
this->button2->TabIndex = 10;
this->button2->Text = L"Insert";
this->button2->UseVisualStyleBackColor = false;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
this->button3->AutoSize = true;
this->button3->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)),
static_cast<System::Int32>(static_cast<System::Byte>(255)));
this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button3->Location = System::Drawing::Point(285, 576);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(115, 42);
this->button3->TabIndex = 11;
this->button3->Text = L"Update";
this->button3->UseVisualStyleBackColor = false;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// button4
//
this->button4->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
this->button4->AutoSize = true;
this->button4->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->button4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button4->Location = System::Drawing::Point(422, 576);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(115, 42);
this->button4->TabIndex = 12;
this->button4->Text = L"Delete";
this->button4->UseVisualStyleBackColor = false;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// idlbl
//
this->idlbl->AutoSize = true;
this->idlbl->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->idlbl->Location = System::Drawing::Point(140, 26);
this->idlbl->Name = L"idlbl";
this->idlbl->Size = System::Drawing::Size(0, 24);
this->idlbl->TabIndex = 13;
//
// code
//
this->code->AutoSize = true;
this->code->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->code->Location = System::Drawing::Point(50, 67);
this->code->Name = L"code";
this->code->Size = System::Drawing::Size(57, 24);
this->code->TabIndex = 14;
this->code->Text = L"code";
//
// textBox1
//
this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox1->Location = System::Drawing::Point(171, 67);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(147, 29);
this->textBox1->TabIndex = 15;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->AutoSize = true;
this->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(255)));
this->ClientSize = System::Drawing::Size(825, 670);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->code);
this->Controls->Add(this->idlbl);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->dataGridView1);
this->Controls->Add(this->textBox4);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
load_data();
}
Void load_data() {
SqlCommand^ cmd = gcnew SqlCommand("Select * From Table1", conn);
SqlDataAdapter^ da = gcnew SqlDataAdapter(cmd);
DataTable^ dt = gcnew DataTable();
da->Fill(dt);
dataGridView1->DataSource = dt;
dataGridView1->ColumnHeadersDefaultCellStyle->Font = gcnew::System::Drawing::Font("Tahoma", 12, FontStyle::Bold);
dataGridView1->DefaultCellStyle->Font = gcnew::System::Drawing::Font("Arial", 12);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
textBox1->Text = "";
textBox2->Text = "";
textBox3->Text = "";
textBox4->Text = "";
idlbl->Text = "";
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
for (int item = 0; item < dataGridView1->Rows->Count; item++)
{
if (dataGridView1->Rows[item]->IsNewRow)
continue;
if (textBox1->Text == dataGridView1->Rows[item]->Cells[1]->Value->ToString())
{
MessageBox::Show("Code exists before");
return;
}
}
SqlCommand^ cmd = gcnew SqlCommand("Insert Into Table1(code,fname,lname,sum1)Values(@code,@fname,@lname,@sum1)", conn);
cmd->Parameters->AddWithValue("@code", textBox1->Text->Trim());
cmd->Parameters->AddWithValue("@fname", textBox2->Text->Trim());
cmd->Parameters->AddWithValue("@lname", textBox3->Text->Trim());
cmd->Parameters->AddWithValue("@sum1", textBox4->Text->Trim());
conn->Open();
cmd->ExecuteNonQuery();
conn->Close();
MessageBox::Show("Inserted Successfully");
load_data();
}
private: System::Void dataGridView1_CellClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
int index;
index = e->RowIndex;
DataGridViewRow^ row = dataGridView1->Rows[index];
idlbl->Text = row->Cells[0]->Value->ToString();
textBox1->Text = row->Cells[1]->Value->ToString();
textBox2->Text = row->Cells[2]->Value->ToString();
textBox3->Text = row->Cells[3]->Value->ToString();
textBox4->Text = row->Cells[4]->Value->ToString();
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
if (dataGridView1->SelectedRows->Count == 0)
{
MessageBox::Show("Please Select row to Update.");
return;
}
String^ originalcode = dataGridView1->SelectedRows[0]->Cells[1]->Value->ToString();
for (int item = 0; item < dataGridView1->Rows->Count; item++)
{
if (dataGridView1->Rows[item]->IsNewRow)
continue;
String^ currentcode = dataGridView1->Rows[item]->Cells[1]->Value->ToString();
if (textBox1->Text == currentcode && currentcode != originalcode)
{
MessageBox::Show("code exists before");
return;
}
}
SqlCommand^ cmd = gcnew SqlCommand("Update Table1 Set code=@code,fname=@fname,lname=@lname,sum1=@sum1 Where id=@id", conn);
cmd->Parameters->AddWithValue("code", textBox1->Text->Trim());
cmd->Parameters->AddWithValue("@fname", textBox2->Text->Trim());
cmd->Parameters->AddWithValue("@lname", textBox3->Text->Trim());
cmd->Parameters->AddWithValue("@sum1", textBox4->Text->Trim());
cmd->Parameters->AddWithValue("@id", idlbl->Text);
conn->Open();
cmd->ExecuteNonQuery();
conn->Close();
MessageBox::Show("updated Successfully");
load_data();
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
if (dataGridView1->SelectedRows->Count == 0)
{
MessageBox::Show("Please Select row to delete.");
return;
}
String^ idtodelete = dataGridView1->SelectedRows[0]->Cells[0]->Value->ToString();
SqlCommand^ cmd = gcnew SqlCommand("Delete From Table1 Where id=@id", conn);
cmd->Parameters->AddWithValue("id", idtodelete);
conn->Open();
cmd->ExecuteNonQuery();
conn->Close();
MessageBox::Show("Deleted Successfully");
load_data();
textBox1->Text = "";
textBox2->Text = "";
textBox3->Text = "";
textBox4->Text = "";
idlbl->Text = "";
}
};
}