Gridview code sorting by PAGE, select, edit, cancel, delete, positive and negative bidirectional sorting, drop-down menu dropdownlist combined

Source: Internet
Author: User
Current content: the gridview has no code to sort by page. Select, edit, and cancel the gridview. Delete the combination of the positive and negative order of the gridview and the drop-down menu dropdownlist. 1. pagination without code in the gridview: 1. allowsorting is set to true, and 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, the value 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: 1 using system; 2 using system. data; 3 using system. configuration; 4 using system. web; 5 using system. web. security; 6 using system. web. ui; 7 using system. web. UI. webcontrols; 8 using system. web. UI. webcontrols. webparts; 9 using system. web. UI. htmlcontrols; 10 using system. data. sqlclient; 1112 public partial class _ default: system. web. UI. page 13 {14 // clear moon http://blog.csdn.net/21aspnet 15 sqlconnection sqlcon; 16 sqlcommand sqlcom; 17 string strcon = "Data Source = (local); database = database name; uid = Account; Pwd = password"; 18 protected void page_load (Object sender, eventargs e) 19 {20 if (! Ispostback) 21 {22 BIND (); 23} 24} 25 protected void gridview1_rowediting (Object sender, gridviewediteventargs e) 26 {27 gridview1.editindex = E. neweditindex; 28 BIND (); 29} 3031 // Delete 32 protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E) 33 {34 string sqlstr = "delete from table where id = '" + gridview1.datakeys [E. rowindex]. value. tostring () + "'"; 35 sqlcon = new sqlconnection (strcon ); 36 sqlcom = new sqlcommand (sqlstr, sqlcon); 37 sqlcon. open (); 38 sqlcom. executenonquery (); 39 sqlcon. close (); 40 BIND (); 41} 4243 // update 44 protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs e) 45 {46 sqlcon = new sqlconnection (strcon ); 47 string sqlstr = "Update table set field 1 = '" 48 + (textbox) (gridview1.rows [E. rowindex]. cells [1]. controls [0]). text. tostring (). trim () + "', Field 2 ='" 49 + (T Extbox) (gridview1.rows [E. rowindex]. cells [2]. controls [0]). text. tostring (). trim () + "', Field 3 ='" 50 + (textbox) (gridview1.rows [E. rowindex]. cells [3]. controls [0]). text. tostring (). trim () + "'where id = '" 51 + gridview1.datakeys [E. rowindex]. value. tostring () + "'"; 52 sqlcom = new sqlcommand (sqlstr, sqlcon); 53 sqlcon. open (); 54 sqlcom. executenonquery (); 55 sqlcon. close (); 56 gridview1.editindex =-1; 57 BIND (); 58} 5960 // cancel 61 protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs e) 62 {63 gridview1.editindex =-1; 64 BIND (); 65} 6667 // bind 68 public void BIND () 69 {70 string sqlstr = "select * from table"; 71 sqlcon = new sqlconnection (strcon ); 72 sqldataadapter myda = new sqldataadapter (sqlstr, sqlcon); 73 dataset myds = new dataset (); 74 sqlcon. open (); 75 myda. fill (myds, "table"); 76 gridview 1. datasource = myds; 77 gridview1.datakeynames = new string [] {"ID"}; // primary key 78 gridview1.databind (); 79 sqlcon. close (); 80} 81} 8283 main front-end code: 1 <asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" cellpadding = "4" 2 forecolor = "#333333" gridlines = "NONE" onrowdeleting = "gridview1_rowdeleting" onrowediting = ""gridview1_rowediting" 3 onrowupdating = "gridview1_rowupdating" onrowcancelingedit = "GRI Dview1_rowcancelingedit "> 4 <footerstyle backcolor =" #990000 "font-bold =" true "forecolor =" white "/> 5 <columns> 6 <asp: boundfield datafield = "ID card number" headertext = "User ID" readonly = "true"/> 7 <asp: boundfield datafield = "name" headertext = "User Name"/> 8 <asp: boundfield datafield = "employee gender" headertext = "gender"/> 9 <asp: boundfield datafield = "Home Address" headertext = "Home Address"/> 10 <asp: commandfield headertext = "select" showselectbutton = "True"/> 11 <asp: commandfield headertext = "edit" showeditbutton = "true"/> 12 <asp: commandfield headertext = "delete" showdeletebutton = "true"/> 13 </columns> 14 <rowstyle forecolor = "#000066"/> 15 <selectedrowstyle backcolor = "#669999" font- bold = "true" forecolor = "white"/> 16 <pagerstyle backcolor = "white" forecolor = "#000066" horizontalalign = "Left"/> 17 

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.