Use dataset to update content in the gridview

Source: Internet
Author: User

 

  Private   Void BIND ()
{
Sqlconnection con =   New Sqlconnection (configurationmanager. connectionstrings [ " Connstr " ]. Connectionstring );
Sqldataadapter SDA =   New Sqldataadapter ( " Select * form name " , Con );
Dataset DS =   New Dataset ();
SDA. Fill (DS, " Temp " );
Con. Close ();
Gridview1.datasource = DS. Tables [ " Temp " ]. Defaultview;
Gridview1.databind ();
}  
Private   Void Fill ( Int ID, String Name, Int Age)
{
Sqlconnection con =   New Sqlconnection (configurationmanager. connectionstrings
[ " Connstr " ]. Connectionstring );
Sqldataadapter SDA =   New Sqldataadapter ( " Select * from name " , Con );

Sqlcommandbuilder scbld =   New Sqlcommandbuilder (SDA );
// If you do not see the above sentence, then dataset will be able to update only selete
Dataset DS =   New Dataset ();
Try
{
SDA. Fill (DS, " Temp " );

DS. Tables [ " Temp " ]. Defaultview. Sort =   " ID " ;
// Sort by ID
Int Index = DS. Tables [ " Temp " ]. Defaultview. Find (ID );
// Locate the index of the row where the data we want is located
DS. Tables [ " Temp " ]. Rows [Index] [ " Name " ] = Name;
DS. Tables [ " Temp " ]. Rows [Index] [ " Age " ] = Age;
// The data in dataset must be updated using arrays.

Int Rows = SDA. Update (DS, " Temp " );
Response. Write ( " Updated successfully "   + Rows +   " Row data " );
}
Catch (Exception E)
{
Response. Write ("The cause of the error is:" +E. Message );
}
}
Protected   Void Gridview1_rowupdating ( Object Sender, gridviewupdateeventargs E)
{
Int Index = E. rowindex;
Int ID = Convert. toint32 (gridview1.rows [Index]. cells [ 1 ]. Text );
// The above indicates that the row number of gridview1 needs to be dynamically retrieved, while the column number is fixed.
String Name = (Textbox) gridview1.rows [Index]. cells [ 2 ]. Findcontrol ( " Textbox1 " ). Text;
Int Age = Convert. toint32 (textbox) gridview1.rows [Index]. cells [ 3 ]. Findcontrol ( " Textbox2 " ). Text );
Fill (ID, name, age );
Gridview1.editindex =   - 1 ;
BIND ();
}

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.