Datagridview update database

Source: Internet
Author: User
Use the datagridview to update a database

I am used to the convenient operation of DBGrid in BCB and want to do the same in vs C #, but it is really not easy. I have spent a lot of effort and I am better at it.
But it is quite troublesome. This dview is not easy.
You can click the button to submit the data...

Public Partial Class Form1: Form
{
Form1 data member # RegionForm1 data member
PrivateDatatable dt= NewDatatable ();
PrivateSqldataadapter SDA= NewSqldataadapter ();
# Endregion

form1 constructor # RegionForm1 Constructor
PublicForm1 ()
{
Initializecomponent ();
}
# Endregion

Connect to the database and display data # Region Connect to the database and display data
Private   Void Form1_load ( Object Sender, eventargs E)
{

Sqlconnection Conn =   New Sqlconnection ( " Server = 127.0.0.1; database = pubs; uid = sa " );
Sqlcommand SCD =   New Sqlcommand ( " Select * from tables " , Conn );
SDA. selectcommand = SCD;
SDA. Fill (DT );
Datagridview1.datasource = DT;
}
# Endregion

Update Database # Region Update Database
Private   Void Toolstripbutton#click ( Object Sender, eventargs E)
{
Try
{
Sqlcommandbuilder SCB= NewSqlcommandbuilder (SDA );
SDA. Update (DT );
}
Catch (System. Exception ex)
{
MessageBox. Show (ex. tostring ());
Return;
}
MessageBox. Show ( " Update successful! " );
}
# Endregion

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.