GridView控件中自动求和、合并单元格和排序

[1] ASP.NET 专栏收录该内容
19 篇文章 0 订阅

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView控件中自动求和、合并单元格和排序.aspx.cs" Inherits="GridView控件中自动求和_合并单元格和排序" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
      <title>无标题页</title>
</head>
<body>
      <form id="form1" runat="server">
      <div>
      <table align="center">
      <tr>
         <td><b>学生成绩信息</b></td>
      </tr>
      <tr>
         <td>
       
         <asp:GridView ID="GridView1" runat="server" AllowSorting="true"    AutoGenerateColumns="false"     OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting">
           <Columns>
              <asp:BoundField DataField="stuname"    HeaderText="学生姓名" SortExpression="stuname"/>
              <asp:BoundField DataField="stusex"    HeaderText="学生性别" SortExpression="stusex" />
              <asp:BoundField DataField="which_lesson"    HeaderText="考试科目" SortExpression="which_lesson"/>
              <asp:BoundField DataField="taotiname" HeaderText="套题名称" SortExpression="taotiname"/>
              <asp:BoundField DataField="res_total" HeaderText="分数" SortExpression="res_total"/>
           </Columns>
         </asp:GridView>
         </td>
      </tr>
      <tr>
         <td></td>
      </tr>
      </table>
      </div>
      </form>
</body>
</html>

***************************************************

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class GridView控件中自动求和_合并单元格和排序 : System.Web.UI.Page
{
      int sum = 0;
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              ViewState["orderby"] = "stuname";
              ViewState["asc"] = "asc";
              Cache.Remove("ds");
              bind();
            
          }
      }
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
      {
          string sPage = e.SortExpression;
          if (ViewState["orderby"].ToString() == sPage)
          {
              if (ViewState["asc"].ToString() == "desc")
                  ViewState["asc"] = "asc";
              else
                  ViewState["asc"] = "desc";
          }
          else
          {
              ViewState["orderby"] = e.SortExpression;
          }
          bind();
      }
      public void bind()
      {
          DataSet myds;
          if (Cache["ds"] == null)
          {
              string sqlstr = "select * from tb_StuResult";
              string strCon = "Data Source=(local);Database=db_04;Uid=sa;Pwd=123456";
              SqlConnection sqlcon = new SqlConnection(strCon);
              SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
              myds = new DataSet();
              myda.Fill(myds, "tb_StuResult");
              Cache["ds"] = myds;
          }
          else
          {
              myds = (DataSet)Cache["ds"];
          }
          DataView view = myds.Tables["tb_StuResult"].DefaultView;
          string sort = (string)ViewState["orderby"] + " " + (string)ViewState["asc"];
          view.Sort = sort;
          GridView1.ShowFooter = true;
          GridView1.DataSource = view;
          GridView1.DataBind();
          //GridView1.ShowFooter = false;
        
          gvRender();
      }
      //合并单元格
      private void gvRender()
      {
          if (GridView1.Rows.Count < 2)
          {
              return;
          }
          for (int i = 0; i < 2; i++)
          {
              int start = 0, end = 1;
              TableCell tcStart = GridView1.Rows[0].Cells[i];
              TableCell tcEnd;
              while (end < GridView1.Rows.Count)
              {
                  tcEnd = GridView1.Rows[end].Cells[i];//当前行
                  if (tcEnd.Text == tcStart.Text)//当前行的值 等于起始行
                  {
                      tcEnd.Visible = false;                    //当前行不显示
                      tcStart.RowSpan = end - start + 1;        //设置起始行行距
                      tcStart.VerticalAlign = VerticalAlign.Middle;//使起始行居中
                  }
                  else
                  {
                      start = end;
                      tcStart = GridView1.Rows[start].Cells[i];
                  }
                  end++;
              }

          }
      }
      //显示页脚
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowIndex > -1)
          {

              sum+=Convert.ToInt32(e.Row.Cells[4].Text);
          }
          else if(e.Row.RowType==DataControlRowType.Footer)
          {
              e.Row.Cells[1].Text = "总分为";
              e.Row.Cells[2].Text = sum.ToString();
              e.Row.Cells[3].Text = "平均分为:";
              e.Row.Cells[4].Text = ((int)(sum/GridView1.Rows.Count)).ToString();
          }
      }
   
}

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值