C # GridView72 highlights

Source: Internet
Author: User

1. The GridView does not have code to sort by PAGE:

:

1. Set AllowSorting to True, and the aspx code is AllowSorting = "True ";
2. By default, there are 10 entries on one page. to modify the number of entries on each page, modify the PageSize. In the aspx code, it is PageSize = "12 ".
3. The default value is unidirectional sorting. Right-click the "properties" in the GridView and select AllowSorting as True.


 

2. Select, edit, cancel, and delete the GridView:

:

 

Background code:
You can use sqlhelper, Which is useless in this article. The Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
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;

Public partial class _ Default: System. Web. UI. Page
{

// Clear moon http://blog.csdn.net/21aspnet
SqlConnection sqlcon;
SqlCommand sqlcom;
String strCon = "Data Source = (local); Database = Database name; Uid = Account; Pwd = password ";
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Bind ();
}
}
Protected void gridviewinclurowediting (object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e. NewEditIndex;
Bind ();
}

// Delete
Protected void GridView1_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
String sqlstr = "delete from table where id =" + GridView1.DataKeys [e. RowIndex]. Value. ToString () + "";
Sqlcon = new SqlConnection (strCon );
Sqlcom = new SqlCommand (sqlstr, sqlcon );
Sqlcon. Open ();
Sqlcom. ExecuteNonQuery ();
Sqlcon. Close ();
Bind ();
}

// Update
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
Sqlcon = new SqlConnection (strCon );
String sqlstr = "update table set field 1 ="
+ (TextBox) (GridView1.Rows [e. rowIndex]. cells [1]. controls [0]). text. toString (). trim () + ", Field 2 ="
+ (TextBox) (GridView1.Rows [e. rowIndex]. cells [2]. controls [0]). text. toString (). trim () + ", Field 3 ="
+ (TextBox) (GridView1.Rows [e. RowIndex]. Cells [3]. Controls [0]). Text. ToString (). Trim () + "where id ="
+ GridView1.DataKeys [e. RowIndex]. Value. ToString () + "";
Sqlcom = new SqlCommand (sqlstr, sqlcon );
Sqlcon. Open ();
Sqlcom. ExecuteNonQuery ();
Sqlcon. Close ();
GridView1.EditIndex =-1;
Bind ();
}

// Cancel
Protected void GridView1_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex =-1;
Bind ();
}

// Bind
Public void bind ()
{
String sqlstr = "select * from table ";
Sqlcon = new SqlConnection (strCon );

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.