C# windows reading from datagrid
Hello and thank you for any help.
I am new to C# and am not sure how to read the info. in a datagrid cell after the user has edited it, so that I can insert the new row into the database.
To make things clearer, on form PayableTrans.cs I have 2 labels, a datagrid, and buttons(one of which is used to save the changes, btnSave_Changes).
When the On_Load event fires the form is populated with customer info, and supplimental info. on customer is loaded into the datagrid.
If a user chooses to edit the datagrid and add info., how can I read the new info. to use it in my sql insert statement?
[601 byte] By [
moconnor] at [2007-12-16]
I'd recommend you use data binding and the Visual Studio designer. I assume you're using VS 2003? If so, then try the following:
Drop a SqlDataAdapter on your Form
Use the Wizard to create a connection to your Table (from the Data Tab)
This adds a SqlConnection and SqlDataAdapter to the Form
Right click on the SqlDataAdapter and select "Generate DataSet..."
Give the DataSet a reasonable name
In the designer set DataGrid.DataSource to the DataSet
Also set DataGrid.DataMember to your Table (e.g. Customers)
In your Form.Load, add the following:
this.sqlDataAdapter1.Fill(this.yourDataSetName);
Add the following code to your Save handler (Save button event handler):
this.sqlDataAdapter1.Update(this.yourDataSetName);
Run the application...
You may want to look at this article as well: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/databindingadonet.asp
Note if you are using VS 2005, there are even fewer steps.
Joe Stegman
The Windows Forms Team
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.