首页  ·  知识 ·  云计算
asp.net2.0里的DataGridView的列动态排序
佚名  本站原创  综合  编辑:dezai  图片来源:网络
//这个方法就是用来调整列顺序的具体方法,里面还包含了一个操作列,是TemplateField&nb
 //这个方法就是用来调整列顺序的具体方法,里面还包含了一个操作列,是TemplateField
        private void AdjustGridViewColumns()
        {
            //if (Page.IsPostBack) return;
            PersonSettingController psc = new PersonSettingController();
            CountrySettingController csc = new CountrySettingController();

            List psis = psc.GetByClubId(Common.CurrentClubId);
            PersonSettingInfo psi = new PersonSettingInfo();
            if (psis.Count != 0)
                psi = psis[0];

            CountrySettingInfo csi = new CountrySettingInfo();
            if (psi.DefaultCountry.HasValue)
            {
                List settings = csc.GetByCountryId(Common.CurrentClubId, psi.DefaultCountry.Value);
                if (settings.Count > 0)
                    csi = settings[0];
            }

            //country column visible or not
            this.GridView1.Columns[10].Visible = psi.UseMoreCountries;

            //if (csi == null) return;
            //if (csi.HouseNumberAfterStreet) return; //this is default;

            DataControlFieldCollection cols = this.GridView1.Columns.CloneFields();
            this.GridView1.Columns.Clear();

            TemplateField tf = cols[1] as TemplateField;
            //CommandTemplate 是自己写的一个小类,里面会有两个图形按钮。
            CommandTemplate ct = new CommandTemplate("command");
            ct.CommandArgumentField = "PersonId";
            tf.ItemTemplate = ct;

            //下面开始为动态排序
            for (int i = 0; i < 7; i++)
                this.GridView1.Columns.Add(cols[i]);

            if (!csi.HouseNumberAfterStreet)
            {
                //houseNr.
                this.GridView1.Columns.Add(cols[8]);
                //street
                this.GridView1.Columns.Add(cols[7]);
            }
            else
            {
                //houseNr.
                this.GridView1.Columns.Add(cols[7]);
                //street
                this.GridView1.Columns.Add(cols[8]);
            }

            if (csi.ZipCodeAfterCity)
            {
                //city
                this.GridView1.Columns.Add(cols[10]);
                //zipcode
                this.GridView1.Columns.Add(cols[9]);
            }
            else
            {
                //city
                this.GridView1.Columns.Add(cols[9]);
                //zipcode
                this.GridView1.Columns.Add(cols[10]);
            }

            this.GridView1.Columns.Add(cols[11]);

            cols.Clear();

            //设置一个变量说明已经排序过
            this.ColumnsAdjusted = true;
        }

        //使用ViewState来储存是否已经排过序
        private bool ColumnsAdjusted
        {
            get
            {
                object b = ViewState["ColumnsAdjusted"];
                return (b == null) ? false : true;
            }
            set
            {
                ViewState["ColumnsAdjusted"] = value;
            }
        }


        //这里是重要的,在LoadViewState的时候如果检测到已经排过序,则再重新排序一下。
        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ColumnsAdjusted)
            {
                this.AdjustGridViewColumns();
            }
        }

        //如果不是Postback则在第一次加载的时候排序
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            if (!ColumnsAdjusted)
            {
                this.AdjustGridViewColumns();
            }
         }

        //按道理这里应该不使用的,可是事实上只有这样才能够使用模板列继续存在, 而且能引发事件
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (Page.IsPostBack)
                {
                    CommandTemplate ct = new CommandTemplate("");
                    ct.InstantiateIn(e.Row.Cells[1]);
                }
            }
        }


//CommandTemplate类,继承于ITemplate
using System;
using System.Data;
using System.Configuration;
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;

namespace XCESS.XM
{
    public class CommandTemplate : ITemplate
    {
        private string colname;

        public CommandTemplate(string colname)
        {
            this.colname = colname;
        }

        private string commandargumentfield;

        public string CommandArgumentField
        {
            get { return commandargumentfield; }
            set { commandargumentfield = value; }
        }

        private string commandargument;

        public string CommandArgument
        {
            get { return commandargument; }
            set { commandargument = value; }
        }

        private bool includeView = true;

        public bool IncludeView
        {
            get { return includeView; }
            set { includeView = value; }
        }
        private bool includeEdit = true;

        public bool IncludeEdit
        {
            get { return includeEdit; }
            set { includeEdit = value; }
        }


        public void InstantiateIn(Control container)
        {
            ImageButton btn1 = BuildButton(CommandButtonType.View);
            ImageButton btn2 = BuildButton(CommandButtonType.Edit);

            btn1.DataBinding+=new EventHandler(btn_DataBinding);
            btn2.DataBinding += new EventHandler(btn_DataBinding);

            btn1.ID = "imgView";
            btn2.ID = "btnEdit";

            container.Controls.Add(btn1);
            container.Controls.Add(btn2);
        }

        public void btn_DataBinding(object sender, EventArgs e)
        {
            ImageButton btn = sender as ImageButton;
            GridViewRow container = (GridViewRow)btn.NamingContainer;


            if (!string.IsNullOrEmpty(commandargumentfield))
            {
                btn.CommandArgument = (DataBinder.Eval(container.DataItem, commandargumentfield) ?? "").ToString();
            }
        }

        private ImageButton BuildButton(CommandButtonType commandButtonType)
        {
            ImageButton btn = new ImageButton();
            if (commandButtonType == CommandButtonType.View)
            {
                btn.CommandName = "Select";
                btn.ImageUrl = "~/images/view.gif";
            }
            else
            {
                btn.CommandName = "EditDetail";
                btn.ImageUrl = "~/images/edit.gif";
            }
            btn.CausesValidation = false;

            if (!string.IsNullOrEmpty(commandargument))
                btn.CommandArgument = commandargument;

            return btn;
        }
    }

    public enum CommandButtonType
    {
        Edit,
        View,
    }
}
本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读