تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
عمل datagridview مربوطه بقاعده بيانات للاضافه و الحذف و التعديل سي شارب
#1
كاتب الموضوع : AhmedEssawy

مثال كامل :

كود :
[color=blue][SIZE=2][COLOR=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System;[/color]
[/SIZE][SIZE=2][color=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System.Collections.Generic;[/color]
[/SIZE][SIZE=2][color=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System.ComponentModel;[/color]
[/SIZE][SIZE=2][color=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System.Data;[/color]
[/SIZE][SIZE=2][color=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System.Drawing;[/color]
[/SIZE][SIZE=2][color=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System.Text;[/color]
[/SIZE][SIZE=2][color=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System.Windows.Forms;[/color]
[/SIZE][SIZE=2][color=#0000ff]using[/color][/SIZE][SIZE=2][color=#000000] System.Data.SqlClient;[/color]
[/SIZE][SIZE=2][color=#0000ff]namespace[/color][/SIZE][SIZE=2][color=#000000] WindowsApplication8[/color]
{
[/SIZE][SIZE=2][color=#0000ff]public[/color][/SIZE][SIZE=2][color=#0000ff]partial[/color][/SIZE][SIZE=2][color=#0000ff]class[/color][/SIZE][SIZE=2][color=#008080]Form1[/color][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#008080]Form
[/COLOR][/SIZE][SIZE=2]{
[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#008080]BindingSource[/color][/SIZE][SIZE=2] bindingSource1 = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]BindingSource[/color][/SIZE][SIZE=2]();
[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#008080]SqlDataAdapter[/color][/SIZE][SIZE=2] dataAdapter = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]SqlDataAdapter[/color][/SIZE][SIZE=2]();
[/SIZE][SIZE=2][color=#008080]DataGridView[/color][/SIZE][SIZE=2] dataGridView1 = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]DataGridView[/color][/SIZE][SIZE=2]();
[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#008080]Button[/color][/SIZE][SIZE=2] reloadButton = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]Button[/color][/SIZE][SIZE=2]();
[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#008080]Button[/color][/SIZE][SIZE=2] submitButton = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]Button[/color][/SIZE][SIZE=2]();
[/SIZE][SIZE=2][color=#0000ff]public[/color][/SIZE][SIZE=2] Form1()
{
InitializeComponent();
dataGridView1.Dock = [/SIZE][SIZE=2][color=#008080]DockStyle[/color][/SIZE][SIZE=2].Fill;
reloadButton.Text = [/SIZE][SIZE=2][color=#800000]"reload"[/color][/SIZE][SIZE=2];
submitButton.Text = [/SIZE][SIZE=2][color=#800000]"submit"[/color][/SIZE][SIZE=2];
reloadButton.Click += [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2] System.[/SIZE][SIZE=2][color=#008080]EventHandler[/color][/SIZE][SIZE=2](reloadButton_Click);
submitButton.Click += [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2] System.[/SIZE][SIZE=2][color=#008080]EventHandler[/color][/SIZE][SIZE=2](submitButton_Click);
[/SIZE][SIZE=2][color=#008080]FlowLayoutPanel[/color][/SIZE][SIZE=2] panel = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]FlowLayoutPanel[/color][/SIZE][SIZE=2]();
panel.Dock = [/SIZE][SIZE=2][color=#008080]DockStyle[/color][/SIZE][SIZE=2].Top;
panel.AutoSize = [/SIZE][SIZE=2][color=#0000ff]true[/color][/SIZE][SIZE=2];
panel.Controls.AddRange([/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]Control[/color][/SIZE][SIZE=2][] { reloadButton, submitButton });
[/SIZE][SIZE=2][color=#0000ff]this[/color][/SIZE][SIZE=2].Controls.AddRange([/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]Control[/color][/SIZE][SIZE=2][] { dataGridView1, panel });
[/SIZE][SIZE=2][color=#0000ff]this[/color][/SIZE][SIZE=2].Load += [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2] System.[/SIZE][SIZE=2][color=#008080]EventHandler[/color][/SIZE][SIZE=2](Form1_Load);
[/SIZE][SIZE=2][color=#0000ff]this[/color][/SIZE][SIZE=2].Text = [/SIZE][SIZE=2][color=#800000]"DataGridView databinding and updating demo"[/color][/SIZE][SIZE=2];
}

[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#0000ff]void[/color][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][color=#0000ff]object[/color][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][color=#008080]EventArgs[/color][/SIZE][SIZE=2] e)
{
[/SIZE][SIZE=2][COLOR=#008000]// Bind the DataGridView to the BindingSource
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]// and load the data from the database.
[/COLOR][/SIZE][SIZE=2]dataGridView1.DataSource = bindingSource1;
GetData([/SIZE][SIZE=2][color=#800000]"select * from Customers"[/color][/SIZE][SIZE=2]);

}
[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#0000ff]void[/color][/SIZE][SIZE=2] reloadButton_Click([/SIZE][SIZE=2][color=#0000ff]object[/color][/SIZE][SIZE=2] sender, System.[/SIZE][SIZE=2][color=#008080]EventArgs[/color][/SIZE][SIZE=2] e)
{
[/SIZE][SIZE=2][COLOR=#008000]// Reload the data from the database.
[/COLOR][/SIZE][SIZE=2]GetData(dataAdapter.SelectCommand.CommandText);
}
[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#0000ff]void[/color][/SIZE][SIZE=2] submitButton_Click([/SIZE][SIZE=2][color=#0000ff]object[/color][/SIZE][SIZE=2] sender, System.[/SIZE][SIZE=2][color=#008080]EventArgs[/color][/SIZE][SIZE=2] e)
{
[/SIZE][SIZE=2][COLOR=#008000]// Update the database with the user's changes.
[/COLOR][/SIZE][SIZE=2]dataAdapter.Update(([/SIZE][SIZE=2][color=#008080]DataTable[/color][/SIZE][SIZE=2])bindingSource1.DataSource);
}
[/SIZE][SIZE=2][color=#0000ff]private[/color][/SIZE][SIZE=2][color=#0000ff]void[/color][/SIZE][SIZE=2] GetData([/SIZE][SIZE=2][color=#0000ff]string[/color][/SIZE][SIZE=2] selectCommand)
{
[/SIZE][SIZE=2][COLOR=#0000ff]try
[/COLOR][/SIZE][SIZE=2]{
[/SIZE][SIZE=2][COLOR=#008000]// Specify a connection string. Replace the given value with a
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]// valid connection string for a Northwind SQL Server sample
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]// database accessible to your system.
[/COLOR][/SIZE][SIZE=2][color=#008080]String[/color][/SIZE][SIZE=2] connectionString =
[/SIZE][SIZE=2][color=#800000]"Integrated Security=SSPI;Persist Security Info=False;"[/color][/SIZE][SIZE=2] +
[/SIZE][SIZE=2][color=#800000]"Initial Catalog=Northwind;Data Source=localhost"[/color][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#008000]// Create a new data adapter based on the specified query.
[/COLOR][/SIZE][SIZE=2]dataAdapter = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]SqlDataAdapter[/color][/SIZE][SIZE=2](selectCommand, connectionString);
[/SIZE][SIZE=2][COLOR=#008000]// Create a command builder to generate SQL update, insert, and
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]// delete commands based on selectCommand. These are used to
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]// update the database.
[/COLOR][/SIZE][SIZE=2][color=#008080]SqlCommandBuilder[/color][/SIZE][SIZE=2] commandBuilder = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]SqlCommandBuilder[/color][/SIZE][SIZE=2](dataAdapter);
[/SIZE][SIZE=2][COLOR=#008000]// Populate a new data table and bind it to the BindingSource.
[/COLOR][/SIZE][SIZE=2][color=#008080]DataTable[/color][/SIZE][SIZE=2] table = [/SIZE][SIZE=2][color=#0000ff]new[/color][/SIZE][SIZE=2][color=#008080]DataTable[/color][/SIZE][SIZE=2]();
[/SIZE][SIZE=2][COLOR=#008000]// table.Locale = System.Globalization.CultureInfo.InvariantCulture;
[/COLOR][/SIZE][SIZE=2]dataAdapter.Fill(table);
bindingSource1.DataSource = table;
[/SIZE][SIZE=2][COLOR=#008000]// Resize the DataGridView columns to fit the newly loaded content.
[/COLOR][/SIZE][SIZE=2]dataGridView1.AutoResizeColumns(
[/SIZE][SIZE=2][color=#008080]DataGridViewAutoSizeColumnsMode[/color][/SIZE][SIZE=2].AllCellsExceptHeader);
}
[/SIZE][SIZE=2][color=#0000ff]catch[/color][/SIZE][SIZE=2] ([/SIZE][SIZE=2][color=#008080]SqlException[/color][/SIZE][SIZE=2])
{
[/SIZE][SIZE=2][color=#008080]MessageBox[/color][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][color=#800000]"To run this example, replace the value of the "[/color][/SIZE][SIZE=2] +
[/SIZE][SIZE=2][color=#800000]"connectionString variable with a connection string that is "[/color][/SIZE][SIZE=2] +
[/SIZE][SIZE=2][color=#800000]"valid for your system."[/color][/SIZE][SIZE=2]);
}
}
}
}
[/SIZE][/COLOR]
}}}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  [كود] أريد كود الحفظ والتعديل والحذف في قاعدة بيانات اكسس 2007 libyan_snipe64 2 3,175 21-10-16, 11:30 PM
آخر رد: omartaz
  مثال يشمل عملية الحذف والتعديل والاضافة على قاعدة البيانات RaggiTech 0 1,160 17-10-12, 09:23 PM
آخر رد: RaggiTech
  مثال - ربط جدولين في قاعدة بيانات اكسس من خلال فيجول دوت نت 2008 RaggiTech 0 2,501 17-10-12, 08:36 PM
آخر رد: RaggiTech
  الرسم علي أداة DataGridView RaggiTech 0 422 17-10-12, 07:23 PM
آخر رد: RaggiTech
  كيفية نقل بيانات من قاعدة بيانات اكسس الى ملف اكس ام ال RaggiTech 0 905 17-10-12, 07:12 PM
آخر رد: RaggiTech
  لعمل اتصال نحو قاعدة بيانات MySQL RaggiTech 0 373 17-10-12, 06:09 PM
آخر رد: RaggiTech
  قراءة بيانات من ملف أكسيس إلى DataSet RaggiTech 0 423 17-10-12, 05:45 PM
آخر رد: RaggiTech
  لانشاء قاعده بيانات و التعامل معها من خلال الكود RaggiTech 0 519 17-10-12, 05:24 PM
آخر رد: RaggiTech
  كود لاضافة بيانات حقل معين في قاعدة البيانات الى عنصر list RaggiTech 0 432 17-10-12, 05:15 PM
آخر رد: RaggiTech
  عمل datagridview مربوطه بقاعده بيانات للاضافه و الحذف و التعديل فجوال بيزك RaggiTech 0 851 17-10-12, 05:10 PM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم