Implementation of the next standard gridview function in Asp.net 2.0 (without the datasource Control)-. Net tutorial, ASP. NET development

Source: Internet
Author: User

In Asp.net 2.0, the gridview is very convenient. if you add a datasource control, you can immediately bind it to the gridview, which is very convenient. But it does.
If datatable or dataview is used, the datasource control is not used. The following describes how to implement page turning and column sorting of the gridview control in Asp.net 2.0,
Editing function.
First, we read the employee table in the northwind database. After placing a gridview, add several bound columns, Code As follows:
<Asp: gridview id = "gridview1" runat = "server" allowpaging = "true" allowsorting = "true"
Autogeneratecolumns = "false" cellpadding = "4" forecolor = "#333333" gridlines = "NONE"
Width = "100%" datakeynames = "employeeid" onpageindexchanging = "inline" onrowediting = "inline" onrowupdating = "inline" onsorting = "inline" pagesize = "3" onrowcancelingedit = "inline" onrowcommand = "gridview1_rowcommand">
<Footerstyle backcolor = "#990000" font-bold = "true" forecolor = "white"/>
<Columns>
<Asp: boundfield datafield = "employeeid" headertext = "employee ID" readonly = "true"/>
<Asp: boundfield datafield = "firstname" headertext = "first name" sortexpression = "firstname"/>
<Asp: boundfield datafield = "lastname" headertext = "last name" sortexpression = "lastname"/>
<Asp: commandfield showeditbutton = "true"/>
</Columns>
<Rowstyle backcolor = "# fffbd6" forecolor = "#333333"/>
<Selectedrowstyle backcolor = "# ffcc66" font-bold = "true" forecolor = "Navy"/>
<Pagerstyle backcolor = "# ffcc66" forecolor = "#333333" horizontalalign = "center"/>
<Headerstyle backcolor = "#990000" font-bold = "true" forecolor = "white"/>
<Alternatingrowstyle backcolor = "white"/>
</ASP: gridview>

First, we need to implement paging, set allowpaging to true, set the number of pages per page, and finally write in codebehind
Protected void gridview1_pageindexchanging (Object sender, gridviewpageeventargs E)
{
Gridview1.pageindex = E. newpageindex;
Bindgrid ();
}
To enable auto-click sorting for each column, you can set allowsorting = true and onsorting = "gridview1_sorting", where gridview_sorting
The code is
Protected void gridviewinclusorting (Object sender, gridviewsorteventargs E)
{
Viewstate ["sortexpression"] = E. sortexpression;

If (viewstate ["sortdirection"] = NULL)
{
Viewstate ["sortdirection"] = "ASC ";
}
Else
{
If (viewstate ["sortdirection"]. tostring () = "ASC ")
{
Viewstate ["sortdirection"] = "DESC ";
}
Else
{
Viewstate ["sortdirection"] = "ASC ";
}
}
Bindgrid ();
}
Obviously, setting viewsate to save the order of each sort is easy to understand.
Finally, the Edit function is implemented because onrowediting = "gridview1_rowediting" has been set on the ASPX page. The gridview1_rowediting code is
Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
Int empid;
String fname, lname;
Empid = int. parse (gridview1.rows [E. rowindex]. cells [0]. Text );
Fname = (textbox) gridview1.rows [E. rowindex]. cells [1]. controls [0]). text;
Lname = (textbox) gridview1.rows [E. rowindex]. cells [2]. controls [0]). text;

sqlconnection CNN = new sqlconnection (@ "Data Source = localhost; initial catalog = northwind; user id = sa; Password = 123456");
CNN. open ();
sqlcommand cmd = new sqlcommand ("Update employees set firstname = @ fname, lastname = @ lname where employeeid = @ empid", CNN );
cmd. parameters. add (New sqlparameter ("@ fname", fname);
cmd. parameters. add (New sqlparameter ("@ lname", lname);
cmd. parameters. add (New sqlparameter ("@ empid", empid);
cmd.exe cutenonquery ();
CNN. close ();

gridview1.editindex =-1;
bindgrid ();
}< br> protected void gridview1_rowediting (Object sender, gridviewediteventargs E)
{< br> gridview1.editindex = E. neweditindex;
bindgrid ();
}< br> protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs e)
{< br> gridview1.editindex =-1;
bindgrid ();
}

As you can see, the above Code is similar to that of Asp.net 1.1. Finally, the bindgrid () process is very simple.
Dataset DS = new dataset ();
Sqldataadapter da = new sqldataadapter ("select * from employees", @ "Data Source = localhost; initial catalog = northwind; user id = sa; Password = 123456 ");
Da. Fill (DS, "employees ");
Dataview DV = Ds. Tables [0]. defaultview;

If (viewstate ["sortexpression"]! = NULL)
{
DV. Sort = viewstate ["sortexpression"]. tostring () + "" + viewstate ["sortdirection"]. tostring ();
}

Gridview1.datasource = DV;
Gridview1.databind ();

the gridview binds dataview and uses DV. Sort To set the order of each sort. That is to say, the order of each sort remains unchanged.
of course, the last page_load event
protected void page_load (Object sender, eventargs e)
{< br> If (! Ispostback)
{< br> bindgrid ();
}< BR >}

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.