girdview 增添删改,list数据源排序,分页等操作,代码。

 用gridview做数据绑定时,是简单,容易操作,但是感觉数据量大时,就很慢了。gridview,增添删改,list数据源排序,分页等操作的代码:

1.如果绑定的数据源为空时,仍然显示表头,核心代码:

  protected void BindData() { if (DtDataSource.Rows.Count == 0) { DtDataSource.Rows.Add(DtDataSource.NewRow()); this.GridView1.DataSource = DtDataSource; this.GridView1.DataBind(); int columnCount = this.GridView1.Columns.Count; GridView1.Rows[0].Cells.Clear(); GridView1.Rows[0].Cells.Add(new TableCell()); GridView1.Rows[0].Cells[0].ColumnSpan = columnCount; GridView1.Rows[0].Cells[0].Text = "No Record!"; //Set the paging style this.lnkbtnFrist.Enabled = false; this.lnkbtnPre.Enabled = false; this.lnkbtnLast.Enabled = false; this.lnkbtnNext.Enabled = false; } else { DataView dv = new DataView(DtDataSource); dv.Sort = string.Format("{0} {1}", this.Sort_Expression, (this.Sort_Direction == SortDirection.Ascending) ? "ASC" : "DESC"); this.GridView1.DataSource = dv; this.GridView1.DataBind(); }

2,设置每行的格式:

// Set the Row style in the RowDataBound protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowIndex % 2 == 0) { e.Row.Width = 20; e.Row.CssClass = "even"; } else { e.Row.CssClass = "odd"; } } }

3.Add操作:

protected void btnAdd_Click(object sender, EventArgs e) { TextBox empID = GridView1.FooterRow.FindControl("txtID") as TextBox; TextBox empRealName = GridView1.FooterRow.FindControl("txtRealName") as TextBox; DropDownList empSex = GridView1.FooterRow.FindControl("ddlSex") as DropDownList; TextBox empAddress = GridView1.FooterRow.FindControl("txtAddress") as TextBox; string sql = "insert into Employee(EmpID,EmpRealName,EmpSex,EmpAddress) values('" + empID.Text.ToString() + "','" + empRealName.Text.ToString() + "','" + empSex.SelectedValue.ToString() + "','" + empAddress.Text.ToString() + "')"; Common.ExecuteSql(sql); bind(); }  

 4.Delete操作:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlStr = "delete from Employee where ID=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + ""; Common.ExecuteSql(sqlStr); bind(); }

5,Edit,update,cancel edit操作

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string ID = GridView1.DataKeys[e.RowIndex].Value.ToString(); string Emp_ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim(); string Emp_RealName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); string Emp_Sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim(); string Emp_Address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim(); string sqlStr = "update Employee set EmpID='" + Emp_ID + "',EmpRealName='" + Emp_RealName + "',EmpSex='" + Emp_Sex + "',EmpAddress='" + Emp_Address + "' where ID=" + ID + ""; Common.ExecuteSql(sqlStr); GridView1.EditIndex = -1; bind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); }

6,有关的分页排序操作

// Set the css when row data bound protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowIndex % 2 == 0) { e.Row.Width = 20; e.Row.CssClass = "even"; } else { e.Row.CssClass = "odd"; } } // Set the style for paging this.lblCurrentPage.Text = string.Format("Page: {0}/{1}", GridView1.PageIndex + 1, GridView1.PageCount); if (GridView1.PageIndex == 0) { this.lnkbtnFrist.Enabled = false; this.lnkbtnPre.Enabled = false; this.lnkbtnLast.Enabled = true; this.lnkbtnNext.Enabled = true; } else if (GridView1.PageIndex == GridView1.PageCount - 1) { this.lnkbtnNext.Enabled = false; this.lnkbtnLast.Enabled = false; this.lnkbtnFrist.Enabled = true; this.lnkbtnPre.Enabled = true; } else { this.lnkbtnFrist.Enabled = true; this.lnkbtnPre.Enabled = true; this.lnkbtnNext.Enabled = true; this.lnkbtnLast.Enabled = true; } } protected void lnkbtnFrist_Click(object sender, EventArgs e) { this.GridView1.PageIndex = 0; BindData(); } protected void lnkbtnPre_Click(object sender, EventArgs e) { if (this.GridView1.PageIndex > 0) { this.GridView1.PageIndex = this.GridView1.PageIndex - 1; BindData(); } } protected void lnkbtnNext_Click(object sender, EventArgs e) { if (this.GridView1.PageIndex < this.GridView1.PageCount) { this.GridView1.PageIndex = this.GridView1.PageIndex + 1; BindData(); } } protected void lnkbtnLast_Click(object sender, EventArgs e) { this.GridView1.PageIndex = this.GridView1.PageCount; BindData(); } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { if (this.Sort_Direction == SortDirection.Ascending) { this.Sort_Direction = SortDirection.Descending; } else { this.Sort_Direction = SortDirection.Ascending; } this.Sort_Expression = e.SortExpression; BindData(); } private SortDirection Sort_Direction { get { if (ViewState["sortDirection"] == null) return SortDirection.Ascending; else return (SortDirection)ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } private string Sort_Expression { get { if (ViewState["sortExpression"] == null) return "UserName"; else return ViewState["sortExpression"].ToString(); } set { ViewState["sortExpression"] = value; } } protected int PageSize { get { if (ViewState["PageSize"] == null) return 30; else return (int)ViewState["PageSize"]; } set { ViewState["PageSize"] = value; } }

7,设置排序操作的图标显示:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { for (int i = 0; i < GridView1.Columns.Count; i++) { if (GridView1.Columns[i].SortExpression == Sort_Expression) { Image img = new Image(); if (Sort_Direction == SortDirection.Ascending) { img.ImageUrl = "../Images/arrowUp.gif"; img.ToolTip = "Ascending"; } else { img.ImageUrl = "../Images/arrowDown.gif"; img.ToolTip = "Descending"; } e.Row.Cells[i].Controls.Add(img); } } } }

你可能感兴趣的:(String,list,image,object,textbox,sorting)