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

相关文章

MyBatis-Plus逻辑删除实现过程

《MyBatis-Plus逻辑删除实现过程》本文介绍了MyBatis-Plus如何实现逻辑删除功能,包括自动填充字段、配置与实现步骤、常见应用场景,并展示了如何使用remove方法进行逻辑删除,逻辑删... 目录1. 逻辑删除的必要性编程1.1 逻辑删除的定义1.2 逻辑删php除的优点1.3 适用场景2.

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra