GridView的数据行的行合并,GridView实现合计数据项

2024-02-07 08:58

本文主要是介绍GridView的数据行的行合并,GridView实现合计数据项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

以下是页面的HTML代码:
<% @ Page Language="C#" AutoEventWireup="true" StylesheetTheme="Default" Theme="Default"
    CodeFile
="InsuranceList.aspx.cs" Inherits="Employee_InsuranceList" 
%>

<! 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" >
        
< fieldset >
            
< legend > 员工保险管理 </ legend >
            
< div  id ="tabsF" >
                
< ul >
                    
< li >< href ="AddInsurance.aspx"  title ="添加员工保险" >< span > 添加员工保险 </ span ></ a ></ li >
                    
< li >< href ="../Default.aspx"  title ="返回主页面" >< span > 返回 </ span ></ a ></ li >
                
</ ul >
            
</ div >
        
</ fieldset >
        
< fieldset >
            
< legend > 保险信息 </ legend > 开始年份: < asp:TextBox  ID ="txtBDate"  runat ="server"   />
            
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator1"  runat ="server"  ControlToValidate ="txtBDate"
                ErrorMessage
="开始年份不能为空"  Display ="Dynamic" > * </ asp:RequiredFieldValidator >
            
< asp:RegularExpressionValidator  ID ="RegularExpressionValidator1"  runat ="server"  ControlToValidate ="txtBDate"
                Display
="Dynamic"  ErrorMessage ="开始年份格式不正确"  ValidationExpression ="^[0-9]*[1-9][0-9]*$" > * </ asp:RegularExpressionValidator > 结束年份:
            
< asp:TextBox  ID ="txtEndDate"  runat ="server" ></ asp:TextBox >
            
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator2"  runat ="server"  ControlToValidate ="txtEndDate"
                ErrorMessage
="结束年份不能为空"  Display ="Dynamic" > * </ asp:RequiredFieldValidator >
            
< asp:RegularExpressionValidator  ID ="RegularExpressionValidator2"  runat ="server"  ControlToValidate ="txtEndDate"
                Display
="Dynamic"  ErrorMessage ="结束年份格式不正确"  ValidationExpression ="^[0-9]*[1-9][0-9]*$" > * </ asp:RegularExpressionValidator >
            
< asp:CompareValidator  ID ="CompareValidator1"  runat ="server"  ControlToCompare ="txtEndDate"
                ControlToValidate
="txtBDate"  ErrorMessage ="开始年份不能大于结束年份"  Operator ="LessThanEqual"
                Type
="Integer"  Display ="Dynamic" > * </ asp:CompareValidator >
            
< asp:Button  ID ="btnSel"  runat ="server"  Text ="开始查询"  OnClick ="btnSel_Click"   />
            
< asp:GridView  ID ="gvInsurance"  runat ="server"  SkinID ="Default_GridView"   AutoGenerateColumns ="False"
                OnDataBound
="gvInsurance_DataBound"  OnRowDataBound ="gvInsurance_RowDataBound"  CaptionAlign ="Left"  OnRowDeleting ="gvInsurance_RowDeleting"  OnRowEditing ="gvInsurance_RowEditing"  ShowFooter ="True" >
                
< Columns >
                    
< asp:BoundField  DataField ="ComName"  HeaderText ="公司名称"   />
                    
< asp:BoundField  DataField ="DepartName"  HeaderText ="部门名称"   />
                    
< asp:BoundField  DataField ="EmpName"  HeaderText ="员工姓名"   />
                    
< asp:BoundField  DataField ="InsYear"  HeaderText ="保险年度"   />
                    
< asp:BoundField  DataField ="Endowment"  DataFormatString ="{0:C}"  HeaderText ="养老保险"
                        HtmlEncode
="False" >
                        
< ItemStyle  HorizontalAlign ="Right"   />
                    
</ asp:BoundField >
                    
< asp:BoundField  DataField ="Medicare"  DataFormatString ="{0:C}"  HeaderText ="医疗保险"  HtmlEncode ="False" >
                        
< ItemStyle  HorizontalAlign ="Right"   />
                    
</ asp:BoundField >
                    
< asp:BoundField  DataField ="Unemployed"  DataFormatString ="{0:C}"  HeaderText ="失业保险"
                        HtmlEncode
="False" >
                        
< ItemStyle  HorizontalAlign ="Right"   />
                    
</ asp:BoundField >
                    
< asp:BoundField  DataField ="Fund"  DataFormatString ="{0:C}"  HeaderText ="公积金"  HtmlEncode ="False" >
                        
< ItemStyle  HorizontalAlign ="Right"   />
                    
</ asp:BoundField >
                    
< asp:BoundField  DataField ="EmpCode"  HeaderText ="员工编号"   />
                    
< asp:CommandField  ShowEditButton ="True"   />
                    
< asp:CommandField  ShowDeleteButton ="True"   />
                
</ Columns >
            
</ asp:GridView >
        
</ fieldset >
        
< asp:ValidationSummary  ID ="ValidationSummary1"  runat ="server"  ShowMessageBox ="True"
            ShowSummary
="False"   />
    
</ form >
</ body >
</ html >

 实现数据行合并是在GridView的DataBind事件进行书写:
代码如下:

protected   void  gvInsurance_DataBound( object  sender, EventArgs e)
    
{
        GroupRows(
this.gvInsurance, 0);
        GroupRows(
this.gvInsurance, 1);
    }

    
public   static   void  GroupRows(GridView GridView1,  int  cellNum)
    
{
        
int i = 0, rowSpanNum = 1;
        
while (i < GridView1.Rows.Count - 1)
        
{
            GridViewRow gvr 
= GridView1.Rows[i];
            
for (++i; i < GridView1.Rows.Count; i++)
            
{
                GridViewRow gvrNext 
= GridView1.Rows[i];
                
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                
{
                    gvrNext.Cells[cellNum].Visible 
= false;
                    rowSpanNum
++;
                }

                
else
                
{
                    gvr.Cells[cellNum].RowSpan 
= rowSpanNum;
                    rowSpanNum 
= 1;
                    
break;
                }


                
if (i == GridView1.Rows.Count - 1)
                
{
                    gvr.Cells[cellNum].RowSpan 
= rowSpanNum;
                }

            }

        }

    }
书写实现数据列合计是在RowDataBind事件里面进行书写,代码如下:
protected   void  gvInsurance_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Attributes.Add(
"onmouseover""e=this.style.backgroundColor; this.style.backgroundColor='linen'");
            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=e");
        }

        
//sum info
        if (e.Row.RowType == DataControlRowType.Footer)
        
{
            
//unite cell
            e.Row.Cells[0].ColumnSpan = 11;
            
//visual 10 cell 
            for (int i = 1; i < 11; i++)
            
{
                e.Row.Cells[i].Visible 
= false;
            }

            
//get every unite cell value
            TableCell cellEndowment = e.Row.Cells[4];
            TableCell cellMedicare 
= e.Row.Cells[5];
            TableCell cellUnemployed
=e.Row.Cells[6];
            TableCell cellFund
=e.Row.Cells[7];
            TableCell cellSum 
= e.Row.Cells[8];
            
//define variable
            double endowment,medicare,unemployee,fund;
            
//set value
            endowment= GetSum(4);
            medicare
=GetSum(5);
            unemployee
=GetSum(6);
            fund
=GetSum(7);
            
//response string
            e.Row.Cells[0].Text="养老保险合计:" + String.Format("{0:C}",endowment)+
                
" 医疗保险合计:" + String.Format("{0:C}", medicare)+
                
" 失业保险合计:" + string.Format("{0:C}",unemployee)+
                
" 公积金合计:" + string.Format("{0:C}",fund)+
                
" 总计:"+string.Format("{0:C}",this.GetSum(endowment,medicare,unemployee,fund));

        }

    }

注:设置GridView的DataSource时,根据你欲合并数据行的列进行排序。
附1: 

/// <summary>
    
/// get sum 
    
/// </summary>
    
/// <param name="i">column id</param>
    
/// <returns>sum value</returns>

     private   double  GetSum( int  i)
    
{
        
//define variable
        double sum = 0;
        
foreach (GridViewRow row in this.gvInsurance.Rows)
        
{
            sum 
+= Convert.ToDouble(string.Format("{0:2F}",row.Cells[i].Text.Remove(0,1)));
            
        }

        
return sum;
    }

    
/// <summary>
    
/// get all sum
    
/// </summary>
    
/// <param name="endowment">养老保险</param>
    
/// <param name="medicare">医疗保险</param>
    
/// <param name="unemployee">失业保险</param>
    
/// <param name="fund">公积金</param>
    
/// <returns>sum</returns>

     private   double  GetSum( double  endowment, double  medicare, double  unemployee, double  fund)
    
{
        
return endowment+medicare+unemployee+fund;
    }

这篇关于GridView的数据行的行合并,GridView实现合计数据项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/687196

相关文章

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

基于Python实现读取嵌套压缩包下文件的方法

《基于Python实现读取嵌套压缩包下文件的方法》工作中遇到的问题,需要用Python实现嵌套压缩包下文件读取,本文给大家介绍了详细的解决方法,并有相关的代码示例供大家参考,需要的朋友可以参考下... 目录思路完整代码代码优化思路打开外层zip压缩包并遍历文件:使用with zipfile.ZipFil

Python实现word文档内容智能提取以及合成

《Python实现word文档内容智能提取以及合成》这篇文章主要为大家详细介绍了如何使用Python实现从10个左右的docx文档中抽取内容,再调整语言风格后生成新的文档,感兴趣的小伙伴可以了解一下... 目录核心思路技术路径实现步骤阶段一:准备工作阶段二:内容提取 (python 脚本)阶段三:语言风格调

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测