Asp. NET data binding GridView control tips-Practical tips

Source: Internet
Author: User
Tags connectionstrings

It has to be said that the GridView control is really powerful, a simple control can manage the data very beautiful. A summary of the problems encountered in the GridView control in the two-day task;

①: Randomly display information in a database in the GridView control:

The GridView control has a AutoGenerateColumns property that controls whether the GridView control automatically generates the associated columns at run time, and generally sets this property to False. Because what we need is a DIY GridView control. Then click the arrow in the upper-right corner, select Edit column to add a BoundField field, select the data DataField attribute, and then fill in the column name of the column you want to display in the database, and in the Appearance HeaderText property, fill in the column name that you want to display in the database. Then click on the OK control to display the following image:

Then add the link Database code in the ASP background OK. About linked Database code bloggers in the Bowen "ASP" with the GridView control to connect to the SQL Server database has been introduced in detail, this article will not say more.

②: Implementing edit deletion in the GridView control:

Click the arrow in the upper-right corner of the GridView control, select Edit column, add CommandField field, set this field Behavior property Showdeletebutton and ShowEditButton to True. Click OK. The result is as shown in the figure below:

However, editing deletes will not have any functionality at this time. Because there are a lot of events in the GridView control, implementing the edit deletion function is to trigger the appropriate event to be used.

First, we introduce the first event--rowediting. Click Edit to change and cancel when you run the page. The purpose of this event is to display updates and cancellations when you click Edit. Rowcancelingedit. When you run the page click Edit will appear "Change" and "Cancel", the results of the operation as shown in the following figure:

Double-click this event to add code in the background as follows:

   protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e)

    {

      Gridview1.editindex = e. Neweditindex;

      This.shuaxin ();

    }

The second event,--rowcancelingedit event Rowcancelingedit, is to implement the cancellation feature. Double-click this event to fill in the code as follows:

   protected void Gridview1_rowcancelingedit (object sender, Gridviewcancelediteventargs e)

   {

    Gridview1.editindex =-1;

    This.shuaxin ();

   }

The third event--rowupdating implement the Update feature, double-click the event to add the following code:

    protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e)

    {this

      . Gridview1.editindex = E.rowindex;

      string title = Gridview1.datakeys[e.rowindex]. Value.tostring ();

      String cotent = ((TextBox) (Gridview1.rows[e.rowindex]. CELLS[1]. Controls[0]). Text;

 

      String strSQL = "Update activities set cotent= '" + cotent + "'

               where title= '" + title + "";

      SqlConnection con = new SqlConnection (ConfigurationManager.

                connectionstrings["username"]. ConnectionString);

      SqlCommand cmd = new SqlCommand (strSQL, con);

      Con. Open ();

      Cmd. ExecuteNonQuery ();

      Con. Close ();

      Gridview1.editindex =-1;

      This.shuaxin ();

    }

The fourth event--rowdeleting. This event implements the Delete feature, and the double-click Event adds code as follows:

    protected void Gridview1_rowdeleting (object sender, Gridviewdeleteeventargs e)

    {

      string title = Gridview1.datakeys[e.rowindex]. Value.tostring ();

 

      string delete = "Delete activities where title= '" + title + "'";

 

      SqlConnection con = new SqlConnection (ConfigurationManager.

                connectionstrings["username"]. ConnectionString);

      SqlCommand cmd = new SqlCommand (delete, con);

      Con. Open ();

      Cmd. ExecuteNonQuery ();

      Con. Close ();

      Gridview1.editindex =-1;

      This.shuaxin ();  Write your own method of linking to the database;

    

Attached: shuaxin (); Code:

    private void Shuaxin ()

    {

      SqlConnection sqlcon = new SqlConnection (ConfigurationManager.

                 connectionstrings["username"]. ConnectionString);

      Sqlcon. Open ();

      SqlDataAdapter da = new SqlDataAdapter (@ "SELECT * from Activities", sqlcon);

      DataSet ds = new DataSet ();

      Da. Fill (DS);

      if (ds. Tables[0]. Rows.Count > 0)

      {

        gridview1.datasource = ds;

        Gridview1.databind ();

      }

      Sqlcon. Close ();

    }

Note: There is a DataKeyNames property in the GridView control, and setting Datakeyname is the primary key for the row data to be fetched when the line is clicked.

To ensure the accuracy of the deletion of the update; If you do not set this property, the following results appear:

The above is about the ASP.net data binding GridView control use techniques, I hope to help you learn.

Related Article

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.