GridView无代码分页排序(8)

2019-04-09 19:54

址\ />

17.GridView加入自动求和求平均值小计

效果图:

解决方案:

private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

if (e.Row.RowIndex >= 0) {

sum += Convert.ToDouble(e.Row.Cells[6].Text); }

else if (e.Row.RowType == DataControlRowType.Footer) {

e.Row.Cells[5].Text = \总薪水为:\ e.Row.Cells[6].Text = sum.ToString(); e.Row.Cells[3].Text = \平均薪水为:\

e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString(); } }

后台全部代码: 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; using System.Drawing;

public partial class Default7 : System.Web.UI.Page {

SqlConnection sqlcon; SqlCommand sqlcom;

string strCon = \北风贸易;Uid=sa;Pwd=sa\ protected void Page_Load(object sender, EventArgs e) {

if (!IsPostBack) {

bind();

} }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {

GridView1.EditIndex = e.NewEditIndex; bind(); }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {

sqlcon = new SqlConnection(strCon);

string sqlstr = \飞狐工作室 set 姓名='\ +

((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + \家庭住址='\

+

((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + \where 身份证号码='\

+ GridView1.DataKeys[e.RowIndex].Value.ToString() + \ sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open();

sqlcom.ExecuteNonQuery(); sqlcon.Close();

GridView1.EditIndex = -1; bind(); }

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {

GridView1.EditIndex = -1; bind(); }

public void bind() {

string sqlstr = \飞狐工作室\ sqlcon = new SqlConnection(strCon);

SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open();

myda.Fill(myds, \飞狐工作室\ GridView1.DataSource = myds;

GridView1.DataKeyNames = new string[] { \身份证号码\ GridView1.DataBind(); sqlcon.Close(); }

private double sum = 0;//取指定列的数据和

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

if (e.Row.RowIndex >= 0) {

sum += Convert.ToDouble(e.Row.Cells[6].Text); }

else if (e.Row.RowType == DataControlRowType.Footer) {

e.Row.Cells[5].Text = \总薪水为:\ e.Row.Cells[6].Text = sum.ToString(); e.Row.Cells[3].Text = \平均薪水为:\

e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString(); } } }

前台:唯一的花头就是设置ShowFooter=\,否则默认表头为隐藏的!

OnRowUpdating=\

OnRowCancelingEdit=\BackColor=\BorderColor=\BorderStyle=\BorderWidth=\Font-Size=\OnRowDataBound=\ >

18.GridView数据导入Excel/Excel数据读入GridView

效果图:

解决方案:

页面增加一个按钮,单击事件添加如下方法:

protected void Button1_Click(object sender, EventArgs e) {

Export(\学生成绩报表.xls\ }

private void Export(string FileType, string FileName) {

Response.Charset = \

Response.ContentEncoding = System.Text.Encoding.UTF7;

Response.AppendHeader(\\+ HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false;

StringWriter tw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); }

//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内

public override void VerifyRenderingInServerForm(Control control) { }

还有由于是文件操作所以要引入名称空间IO和Text

后台代码: 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; using System.Drawing; using System.IO; using System.Text;

public partial class Default7 : System.Web.UI.Page {

SqlConnection sqlcon; SqlCommand sqlcom;

string strCon = \北风贸易;Uid=sa;Pwd=sa\ protected void Page_Load(object sender, EventArgs e) {

if (!IsPostBack) {

bind();

} }


GridView无代码分页排序(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:第七讲 三角形相似专题小班8份

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: