一个实现自动求和/合并单元格/排序的DataGrid

datagrid|单元格|排序

以前在asp很难实现代码重用,asp.net很好的解决了这个问题,以下是我写的DataGrid,继承DataGrid,加进了升降序/全并单元格/自动求和功能,原理很简单,但很好的实现的代码重用.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
namespace SunService
{
    /// <summary>
    /// Summary description for DataGrid.
    /// </summary>
    [DefaultProperty("Text"),
    ToolboxData("<{0}:DataGrid runat=server></{0}:DataGrid>")]
    public class DataGrid : System.Web.UI.WebControls.DataGrid
    {
        private string text;
        private SqlDataAdapter adp;
        private DataSet ds;
        private DataView view;
        private string[] arritem;
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("")]
        public string Text
        {
            get
            {
                return text;
            }

            set
            {
                text = value;
            }
        }
        /// <summary>
        /// protect SortDirection 排序方向
        /// </summary>

        public string SortDirection
        {
            get
            {
                if(ViewState["SortDirection"]==null)
                {
                    return null;
                }
                else
                {
                    if(ViewState["SortDirection"].ToString()=="")
                    {
                        return null;
                    }
                    else
                    {
                        return ViewState["SortDirection"].ToString();
                    }
                }
            }
            set
            {
                ViewState["SortDirection"]=value;
            }
        }
        /// <summary>
        /// protect SortField 排序字段
        /// </summary>
        public string SortField
        {
            get
            {
                if(ViewState["SortField"]==null)
                {
                    return null;
                }
                else
                {
                    if(ViewState["SortField"].ToString()=="")
                    {
                        return null;
                    }
                    else
                    {
                        return ViewState["SortField"].ToString();
                    }
                }
            }
            set
            {
                ViewState["SortField"]=value;
            }
        }
        /// <summary>
        /// sql  查询字串
        /// </summary>        
        public string selectCommandText
        {
            get
            {
                if(ViewState["selectCommandText"]==null)
                {
                    return null;
                }
                else
                {
                    if(ViewState["selectCommandText"].ToString()=="")
                    {
                        return null;
                    }
                    else
                    {

                        return ViewState["selectCommandText"].ToString();
                    }
                }
            }
            set
            {
                ViewState["selectCommandText"]=value;
            }
        }
        /// <summary>
        /// 连接字串
        /// </summary>
        public string selectConnectionString
        {
            get
            {
                if(ViewState["selectConnectionString"]==null)
                {
                    return null;
                }
                else
                {
                    return ViewState["selectConnectionString"].ToString();
                }
            }
            set
            {
                ViewState["selectConnectionString"]=value;
            }
        }
        public DataTable Bindtable;
        public DataGrid()
        {
            this.Init+=new System.EventHandler(this.DataGrid_Init);
        }
        private void DataGrid_Init(object sender,EventArgs e)
        {

            this.Load+= new System.EventHandler(this.DataGrid_Load);            
            this.SortCommand+=new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid_SortCommand);
            this.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_ItemDataBound);

        }
        private void DataGrid_Load(object sender,EventArgs e)
        {
            this.HorizontalAlign=HorizontalAlign.Center;
            this.AllowSorting=true;
            arritem=new string[256];
            ds=new DataSet();
            
            
        }
    

        /// <summary>
        /// GRID绑定
        /// </summary>
        /// <param name="selectCommandText">查询字串</param>
        /// <param name="selectConnectionString">连接字串</param>
        public void BindGrid(string selectCommandText,string selectConnectionString)
        {
            this.selectCommandText=selectCommandText;
            this.selectConnectionString=selectConnectionString;
            BindGrid();
            
        }
        /// <summary>
        /// grid绑定
        /// </summary>
        /// <param name="selectCommandText">查询字串</param>
        /// <param name="cn">连接对象</param>
        public void BindGrid(string selectCommandText,SqlConnection cn)
        {
            this.selectCommandText=selectCommandText;
            this.selectConnectionString=cn.ConnectionString;
            BindGrid();
        }
        /// <summary>
        /// grid绑定,必须先设置 selectCommmandText 及SelectConnectionString 属性
        /// </summary>
        public void BindGrid()
        {
            if(this.selectCommandText!=null&&this.selectConnectionString!=null)
            {
                adp=new SqlDataAdapter(this.selectCommandText,this.selectConnectionString);
                adp.Fill(ds,"temp");
                view=ds.Tables["temp"].DefaultView;

                if(this.SortField!=null)
                {
                    view.Sort=this.SortField+" "+this.SortDirection;
                    int sortfieldindex=0;
                    for( int  i=0;i<ds.Tables["temp"].Columns.Count;i++)
                    {
                        if(ds.Tables["temp"].Columns[i].ColumnName==this.SortField)
                        {
                            sortfieldindex=i;
                            break;
                        }
                    }
                    string SortDirectionImg="▲";
                    if(this.SortDirection==" DESC")
                    {
                        SortDirectionImg="▼";

                    }
                    if(this.SortField!=this.DataKeyField)
                    {
                        ds.Tables["temp"].Columns[sortfieldindex].ColumnName+=SortDirectionImg;
                    }
                    
                }
                Bindtable=ds.Tables["temp"];
                DataRow row=Bindtable.NewRow();
                row[0]="总计:";                
                for(int i=1;i<Bindtable.Columns.Count;i++)
                {    
                    Type t=Bindtable.Columns[i].DataType;
                    if(t==typeof(Decimal)||t==typeof(Double)||t==typeof(Int16)||t==typeof(Int32)||t==typeof(Int64)||t==typeof(UInt16)||t==typeof(UInt32)||t==typeof(Int64))
                    {
                        row[i]=0;
                        foreach( DataRow r in Bindtable.Rows)
                        {
                            try
                            {
                                row[i]=double.Parse(row[i].ToString())+double.Parse(r[i].ToString());
                            }
                            catch(Exception et)
                            {
                            
                            }
                            
                        }
                    }
                }
                Bindtable.Rows.Add(row);
                
                this.DataSource=view;
                this.DataBind();
                
            }
            else
            {
                
            }
        }
        private void DataGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
            
            if(    this.SortDirection==" DESC")
            {
                this.SortDirection=" ASC";
            }
            else
            {
                this.SortDirection=" DESC";
            }
            
            this.SortField=e.SortExpression;
            this.SortField=this.SortField.Replace("▲","");
            this.SortField=this.SortField.Replace("▼","");
            
            BindGrid();
        }

        private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            try
            {
                string txt="";
                for(int i=0;i<e.Item.Cells.Count;i++)
                {
                    //                    e.Item.Cells[i].Wrap=false;
                    txt=e.Item.Cells[i].Text.Trim();
    
                    if(myClass.IsDouble(txt))
                    {
                        e.Item.Cells[i].HorizontalAlign=HorizontalAlign.Right;
                    }
                    else
                    {
                        if(txt==arritem[i]&&txt!=""&&txt!=null)
                        {
                            e.Item.Cells[i].Text="";
                        }
                        else
                        {
                            arritem[i]=txt;
                        }
                    }
                }
            }
            catch(Exception et)
            {
                
            }

        }

    }
}

调用简单:
把组件拖到页面中 ,假设ID为 DataGrid1:
调用:DataGrid1.BindGrid(string selectCommandText,string selectConnectionString)
这样省了建 conntion DataAdapter DataSet再绑定的时间.
大家还可把显示时间显示格式/数字显示格式等加进ItemDataBound事件中,还有自定义分页功能等.

时间: 2024-09-20 05:46:13

一个实现自动求和/合并单元格/排序的DataGrid的相关文章

word2013如何合并单元格

  合并单元格方法一: 步骤一:选中我们需要合并的多个单元格,然后切换到"表格工具->布局"选项卡,单击"合并"组中的"合并单元格"命令. 步骤二:此时,大家就会看到我们选择的多个单元格已经被合并成了一个. 合并单元格方法二: 步骤一:首先选择需要合并的单元格(我们这里选择第一行单元格) 步骤二:选择 布局 选项卡 步骤三:点击布局选项卡上的合并 单元格 步骤四:所选中的单元格已经被合并了

Repeater后台合并单元格问题

问题描述 前台绑定代码<tablewidth="100%"border="0"align="center"cellpadding="3"cellspacing="1"bgcolor="#bed6e0"id="repeaterTable"><cc1:HHRepeaterID="repXmxx"runat="server&q

wps2016表格有合并单元格如何排序?

  1.首先我们需要对现在的数据备份,以备排序完成之后使用. 2.选中表格中需要参与排序的数据,右击选择"设置单元格格式",或按快捷键"f",在"对齐"选项卡将"文字控制"下的"合并单元格"的勾去掉,然后单击"确定". 3.表格效果如下: 4.选中表格数据,按快捷键"Ctrl+G"打开"定位"对话框,选中"空值"单击"

对Excel2013合并单元格如何排序的方法

  如下图所示,A列是合并单元格,而且都是合并了3个单元格,默认情况下,合并单元格是不能进行排序操作的.那么就没有办法了么?blue1000教你. ①启动Excel2013,看到下面的表格数据,首先选中A2:A13区域,鼠标移动到右下角,出现+号填充柄. ②向右填充,选择仅填充格式.我们不是要改变其内容,只是要个格式而已. ③单击长沙单元格,点击菜单栏--数据--升序排列按钮. ④这样A列就按照首字母顺序进行了排列,但是B.C列还没有取消单元格合并,选择这区域,单击开始--取消单元格合并. ⑤最

java-POI如何修改一个Excel的合并单元格的值(合并单元格原来有值的)?

问题描述 POI如何修改一个Excel的合并单元格的值(合并单元格原来有值的)? POI如何修改一个Excel的合并单元格的值(合并单元格原来有值的)? 解决方案 合并单元格的值其实就是左上角那个单元格的值,直接读写它就可以了. 解决方案二: poi excel合并单元格Apache POI如何获取Excel合并单元格的值POI 合并单元格 值填充问题 解决方案三:

当判断一个单元格是合并单元格时,怎么读取单元格的值呢

问题描述 当判断一个单元格是合并单元格时,怎么读取单元格的值呢,我知道读取合并单元格的第一个单元格的值,但应该如何判断,如何知道哪个才是这个合并单元格的第一个单元格的值呀!?? 解决方案 解决方案二:我是操作的EXCEL表!要循环一行行的读取数据!解决方案三:没人知道么解决方案四:看来问题太简单了,没人喜回复?解决方案五:固定表格的Excel的话,可以定义在数组当中.变化表格的Excel,关注中........解决方案六:这个问题还没解决,再问个问题!~解决方案七:隐藏的工作表读取时知道怎么不读

excel 合并单元格 快捷键

工具 -> 自定义 -> 右键点你在工具栏平时用来合并的按钮 -> 点"总是用文字". 这时平时用来合并的按钮就变成了 "合并及居中(M)",这个(M)就是你想要的快捷方式了,现在点关闭,然后选几个单元格,再按组合键: ALT + M ,怎样? 感觉爽了吧?!~ 处理工作表 Shift+F11 或 Alt+Shift+F1 插入新工作表. Ctrl+Page Down 移动到工作簿中的下一张工作表. Ctrl+Page Up 移动到工作簿中的上一张

excel合并单元格怎么操作以及合并单元格快捷键

现象重现步骤如下: 第一步:在A1:C4区域分别输入数字.实际区域可自己选定,数字也可根据自己的喜好来输. 第二步:选中A1:A4区域,单击"开始"标签"对齐方式"功能区中的"合并居中"命令按钮,在弹出的对话框中单击"确定"按钮完成单元格的合并. 上面两步是一个正常的单元格合并步骤,没有任何问题.最终效果为A1:A4区域合并为一个单元格,并且里面的数值是之前A1单元格中的数值100. 第三步:继续刚才的操作,使合并后的A1单元

Excel合并单元格技巧详解

合并单元格很简单,只需要选取要合并的多个单元格,点合并就可以把单元格合并成一个了.如下图所示. excel2007和excel2010用户可以在功能区点"合并后居中" 快捷键是什么.回答是atl+m,但是在使用时却没有效果,是怎么回事呢? 原因是,需要把合并单元格命令的图标显示形式转换一下.即转换成图形和文本同时显示.可以选取相同数量的单元格-合并单元格-选取合并后单元格后点格式刷-刷向需要保留内容的单元格.操作后单元格合并且单元格内容会被保留下来. 并单元格无法筛选出被合并的内容,但