Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Insert/Update/Delete record with DataGridView... Plz Help!!!!
 

Insert/Update/Delete record with DataGridView... Plz Help!!!!

I have trouble in working with DataGridView. Someone can give me some advise and solution to solve this. I feel confused to work with DataGridView. I want to insert/update/delete record , all in DGV ,
Thank you
Regards
Jerry_lth  Tuesday, October 23, 2007 1:39 PM

Hi Jerry_lth,

You will need to use a DataAdapter and call its Update method to write back to the database. When you call the Update method, the DataAdapter analyzes the changes that have been made and executes the appropriate command (INSERT, UPDATE, or DELETE). When the DataAdapter encounters a change to a DataRow, it uses the InsertCommand, UpdateCommand, or DeleteCommand to process the change.

More information, check this page:
http://msdn2.microsoft.com/en-us/library/33y2221y(VS.80).aspx

I write a simple sample for your information.

Code Block

System.Data.SqlClient;

NewDGV

public partial class Form35 : Form

{

public Form35()

{

InitializeComponent();

}

SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=True");

SqlDataAdapter da = new SqlDataAdapter();

DataTable dt = new DataTable();

private void Form35_Load(object sender, EventArgs e)

{

SqlCommand cm = new SqlCommand("select RegionID,RegionDescription from Region", cn);

da.SelectCommand = cm;

SqlCommandBuilder cmbuilder = new SqlCommandBuilder(da); da.Fill(dt);

this.dataGridView1.DataSource = dt;

}

private void btnSave_Click(object sender, EventArgs e)

{

this.dataGridView1.BindingContext[dt].EndCurrentEdit();

this.da.Update(dt);

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, October 25, 2007 2:52 AM

Hi Jerry_lth,

You will need to use a DataAdapter and call its Update method to write back to the database. When you call the Update method, the DataAdapter analyzes the changes that have been made and executes the appropriate command (INSERT, UPDATE, or DELETE). When the DataAdapter encounters a change to a DataRow, it uses the InsertCommand, UpdateCommand, or DeleteCommand to process the change.

More information, check this page:
http://msdn2.microsoft.com/en-us/library/33y2221y(VS.80).aspx

I write a simple sample for your information.

Code Block

System.Data.SqlClient;

NewDGV

public partial class Form35 : Form

{

public Form35()

{

InitializeComponent();

}

SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=True");

SqlDataAdapter da = new SqlDataAdapter();

DataTable dt = new DataTable();

private void Form35_Load(object sender, EventArgs e)

{

SqlCommand cm = new SqlCommand("select RegionID,RegionDescription from Region", cn);

da.SelectCommand = cm;

SqlCommandBuilder cmbuilder = new SqlCommandBuilder(da); da.Fill(dt);

this.dataGridView1.DataSource = dt;

}

private void btnSave_Click(object sender, EventArgs e)

{

this.dataGridView1.BindingContext[dt].EndCurrentEdit();

this.da.Update(dt);

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, October 25, 2007 2:52 AM
Hi everyone, I'm doing same thing in J#, everything works except this.da.Update(dt); will only update dataTable "dt" but not the data source. How can I update my data source? I'm using MS Access. Thanks in advance


Nik.

Nik_OS  Friday, October 26, 2007 3:33 AM
Thank you Rong-Chun Zhang - MSFT, it is useful for me ....
Best regards
Jerry Lu
Jerry_lth  Saturday, October 27, 2007 5:47 AM

You can use google to search for other answers

Custom Search

More Threads

• Just try too...
• PocketVision error when downloading tasks
• pocket taskvision memory leak
• Can anyone provide the tracker sql scripts?
• TaskVision - Source code installation help needed
• Provider Pattern is here
• C#: DataGridView: Primary Key Column (varchar): On Insert, Column Does Not Allow Nulls
• VS.net 2002 installation errors off issueVision
• Task Vision V2.0 (VB.Net) change language problem!
• Time vs Ticks