DataGrid design

xiaoxiao2021-03-06  105

Head style column Style

< / asp: BoundColumn> time format

DataNavigateUrlField = "ID" DataNavigateUrlFormatString = "? ModifyItem.aspx ID = {0}" HeaderText = "Modify"> < ASP: ButtonColumn Text = "Remove" Headertext = "COMMANDNAME =" Delete "> Sort:

// pass through ViewState [ "SortField"] Sort field public string SortField {set {if (value.ToString () == SortField) SortAscending = SortAscending;! ViewState [ "SortField"] = value;} get {object temp = ViewState ["Sortfield"]; if (temp == null) return string.empty; return temp.tostring ();}}

// Transfer Sort by ViewState ["Sortascending"] Public Bool Sortascending {set {ViewState ["Sortascending"] = BOOL.PARSE (Value.toTString ());} get {Object Temp = ViewState ["Sortascending"]; if (Temp == null) Return True; return bool.parse (ViewState ["sortascending"]. TOSTRING ());}}

// Binding data

private void GridDataBind () {SqlConnection conn = new SqlConnection (ConnectionString); string select = "select * from ItemSurvey order by CreateDate DESC"; SqlDataAdapter da = new SqlDataAdapter (select, conn); DataSet ds = new DataSet (); da. Fill (DS, "Itemsurvey"); DataView DV = New DataView (DS.Tables [0]); DV.Sort = this.Sortfield; if (! This.sortascending) DV.Sort = "DESC"; this.DataGrid1. DataSource = dv; this.DataGrid1.DataBind ();} private void Page_Load (object sender, System.EventArgs e) {// Put user code to initialize the page here if this.GridDataBind () (this.IsPostBack!);} //delete

private void DataGrid1_DeleteCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {string sID = e.Item.Cells [0] .Text; string deleteSql = "delete from ItemSurvey where ID =" sID; if (e .CommandName == "Delete") {SqlConnection conn = new SqlConnection (ConnectionString); try {SqlCommand cmd = new SqlCommand (deleteSql, conn); conn.Open (); cmd.ExecuteNonQuery (); this.GridDataBind ();} catch (Exception err) {this.Response.Write (err.Message.ToString ());} finally {conn.Close ();}}} // tab private void DataGrid1_PageIndexChanged (object source, System.Web.UI.WebControls .DataGridPageChangeDeventargs e) {this.DataGrid1.currentpageindex = E.NewpageIndex; this.griddatabind ();

private void DataGrid1_SortCommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) {this.DataGrid1.CurrentPageIndex = 0; this.SortField = e.SortExpression; this.GridDataBind ();}

/ / Add confirmation on each delete button

private void DataGrid1_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {LinkButton btnDel = new LinkButton ();! if ((e.Item.ItemType == ListItemType.Header || e.Item.ItemType == ListitemType.footer) {btndel = (linkButton) E.Item.cells [11] .controls [0]; btndel.attributes.add ("onclick", "Return Confirm ('OK) To delete /" meaning.Item .Cells [1] .TEXT "/"? '); ");}} // Print Shielding field @media print {.printButton {visibility: hidden;}}

转载请注明原文地址:https://www.9cbs.com/read-123563.html

New Post(0)