国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

一個實現自動求和/合并單元格/排序的DataGrid

2024-07-21 02:16:46
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。以前在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事件中,還有自定義分頁功能等.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇庆市| 冕宁县| 绍兴县| 浮梁县| 满洲里市| 遂宁市| 奉贤区| 松阳县| 佛山市| 荣昌县| 原阳县| 张家川| 平果县| 乐东| 溧阳市| 卢氏县| 龙川县| 宜宾市| 秭归县| 乌兰浩特市| 梅河口市| 五原县| 宜川县| 修水县| 兴安盟| 迁安市| 两当县| 建宁县| 金川县| 闻喜县| 淮安市| 英德市| 宜州市| 龙南县| 延津县| 伊春市| 福泉市| 临沭县| 北安市| 云浮市| 山阴县|