GridView No data source implementation update database (i.e. disconnect update database)

Source: Internet
Author: User

The original Published time: 2008-08-01--from my Baidu article [imported by moving tools]

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

Using System.Data.SqlClient;

GridView No data source implementation update (break update) Drag and drop a GridView when you do not edit this item, you should change its properties Autogeneraltededitbutton this property to true, so that editing this link will appear


public partial class Default3:System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection ("server=.\\sqlexpress;database=test;uid=sa;pwd=123456");
   
    protected void Page_Load (object sender, EventArgs e)
     {
        if (! IsPostBack)
            Bind ();

   }
    private void Bind ()
    {
        SqlDataAdapter SDA = new SqlDataAdapter ("select * from Stu", Conn);
        DataSet ds = new DataSet ();
        SDA. Fill (ds, "temp");
        Gridview1.datasource = ds. tables["Temp"]. DefaultView;
        Gridview1.databind ();
   }
    private void fill (int id, string name, String Banji)
    {
         SqlDataAdapter SDA = new SqlDataAdapter ("select * from Stu", Conn);
        SqlCommandBuilder scbld = new SqlCommandBuilder (SDA);//Because there is this sentence, So when you close the connection, you can also update the

DataSet ds = new DataSet ();
Sda. Fill (ds, "temp");
Ds. tables["Temp"]. Defaultview.sort = "id";
int index = ds. tables["Temp"]. Defaultview.find (ID);
Ds. tables["Temp"]. rows[index]["name"] = name;
Ds. tables["Temp"]. Rows[index]["class"] = Banji;

int rows = SDA. Update (ds, "temp");

Response.Write ("Update" + Rows + "rows datas");
}

protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e)
{
int index = E.rowindex;
int id = Convert.ToInt32 (gridview1.rows[index]. CELLS[1]. Text);
String name = ((TextBox) Gridview1.rows[index]. CELLS[2]. FindControl ("TextBox1")). Text;
String Banji = ((TextBox) Gridview1.rows[index]. CELLS[3]. FindControl ("TextBox2")). Text;
Fill (ID, name, Banji);
Gridview1.editindex =-1;
Bind ();
}
protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e)
{
Gridview1.editindex = E.neweditindex;
Bind ();
}
protected void Gridview1_rowcancelingedit (object sender, Gridviewcancelediteventargs e)
{
Gridview1.editindex =-1;
Bind ();
}
}

GridView No data source implementation update database (i.e. disconnect update database)

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.