Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView -update
 

DataGridView -update

Hi I'm new to C# after working 11 years withVB. I have visual c# 2005 beta on my system. I created the Dategrid View with the following code

SqlConnection oSQLConn = new SqlConnection();

oSQLConn.ConnectionString = "Connection string "

oSQLConn.Open();

 

SqlDataAdapter sqlDa = new SqlDataAdapter("select * from table1", oSQLConn);

//create dataset instance

DataSet dSet = new DataSet();

//fill the dataset

sqlDa.Fill(dSet, "table1");

//bind the data grid with the data set

dataGridView1.DataSource = dSet.Tables["table1"];

oSQLConn.Close();

It allows me to Edit the Data. but did not update the Database. What I have to do ?
Doss

NewtoCsharp2008  Wednesday, September 07, 2005 5:57 PM

Hi,

Yes, you've gotta specify the COmmands needed to send your updates to the database.

SqlDataAdapter sqlDa = new SqlDataAdapter("select * from table1", oSQLConn);
sqlDa.InsertCommand = new SqlCommand("INSERT INTO table1 WHERE myId = @id", oSQLConn);
//Also specify the parameters...
sqlDa.InsertCommand.Parameters.Add("@id",.....);
//also same with the update command
sqlDa.UpdateCommand = new SqlCommand("UPDATE table1 SET ...", oSQLConn);

 

Just search the docs for more details...

 

 

cheers,

Paul June A. Domag

Paul Domag  Friday, September 09, 2005 1:33 AM

Hi,

You'll have to implement the UpdateCommand and DeleteCommand properties of your dataadapter. and call the dataAdapter.Update(dataTable) to send the changes to the database...

 

 

cheers,

Paul June A. Domag

Paul Domag  Thursday, September 08, 2005 10:44 AM
I already have the dataadapter

SqlDataAdapter sqlDa = new SqlDataAdapter("select * from table1", oSQLConn);

Should I do something differant from the above statement
Thanks
DossT

NewtoCsharp2008  Thursday, September 08, 2005 1:10 PM

Hi,

Yes, you've gotta specify the COmmands needed to send your updates to the database.

SqlDataAdapter sqlDa = new SqlDataAdapter("select * from table1", oSQLConn);
sqlDa.InsertCommand = new SqlCommand("INSERT INTO table1 WHERE myId = @id", oSQLConn);
//Also specify the parameters...
sqlDa.InsertCommand.Parameters.Add("@id",.....);
//also same with the update command
sqlDa.UpdateCommand = new SqlCommand("UPDATE table1 SET ...", oSQLConn);

 

Just search the docs for more details...

 

 

cheers,

Paul June A. Domag

Paul Domag  Friday, September 09, 2005 1:33 AM

Hello,

I have a similar problem with a form having a datagridview bound to a SqlExpress table, by dragging a datasource to a form.

The datagridview allows me to enter new rows without any problem. However, when I close the application and start again, the added rows have disappeared, i.e the underlying database is not being updated !!

I have searched the forum and tried different solutions without success. It seems like dragging and dropping a datasource to a form is not enough to produce a form with datagridview for data entry ??!!!

I would be very much grateful if you could point me to a such an example project working properly.

Thank you.

prak_msforum  Tuesday, March 25, 2008 9:43 AM

Hi there,

when editing tables, you are only modifying locally, you need to hit the save button on the top of the grid before closing.

Cheers

JohnConnah  Wednesday, October 07, 2009 10:25 AM

You can use google to search for other answers

Custom Search

More Threads

• howto set "display" text of dg Column headers @ design time?
• How to apply an accelerator key to a button in VBNet 3.0
• MultiColumn ComboBox Column in datagridview
• pros and cons of both Typed and unTyped dataset
• Filtering Data based on a rowfilter applied to a databound table
• combobox and tag property
• DataGridViewCellStyle
• Problem with SQLDependency "Cross-thread operation not valid"
• How to Update The BindingSource?
• Databind a collection and get parent property