DataGridView 实现列自定义顺序,显示或隐藏和记录排列顺序

    用户希望对软件中主要的表格数据列表,可以自定义列自定义顺序,显示或隐藏和记录排列顺序。

    参考:http://www.codeproject.com/Articles/31987/A-DataGridView-Column-Show-Hide-Popup

    增加了新的需求功能。

    实现代码:

    类DataGridViewColumnSelector:   

    public class DataGridViewColumnSelector
    {
        private DataGridView mDataGridView = null;
        private CheckedListBox mCheckedListBox;
        private ToolStripDropDown mPopup;
        private string xmlFile = string.Empty;


        public int MaxHeight = 300;
        public int Width = 200;

        public DataGridView DataGridView
        {
            get { return mDataGridView; }
            set
            {
                if (mDataGridView != null)
                {
                    mDataGridView.CellMouseClick -= new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);
                    mDataGridView.ColumnDisplayIndexChanged -= new DataGridViewColumnEventHandler(mDataGridView_ColumnDisplayIndexChanged);
                }
                mDataGridView = value;
                if (mDataGridView != null)
                {
                    mDataGridView.AllowUserToOrderColumns = true;
                    mDataGridView.ColumnDisplayIndexChanged += new DataGridViewColumnEventHandler(mDataGridView_ColumnDisplayIndexChanged);
                    mDataGridView.CellMouseClick += new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);
                }
            }
        }

        public DataGridViewColumnSelector(DataGridView dgv,string xmlFile)
            : this()
        {
            this.DataGridView = dgv;
            this.xmlFile = xmlFile;
            mDataGridView.ColumnDisplayIndexChanged -= new DataGridViewColumnEventHandler(mDataGridView_ColumnDisplayIndexChanged);
            ReadFromXml();
            mDataGridView.ColumnDisplayIndexChanged += new DataGridViewColumnEventHandler(mDataGridView_ColumnDisplayIndexChanged);
            mDataGridView.ShowCellToolTips = true;
            for (int i = 0; i < mDataGridView.Columns.Count; i++)
            {
                mDataGridView.Columns[i].ToolTipText = Strings.TXT_TABLE_HEADER_TOOLTIP;
            }
        }

        public DataGridViewColumnSelector()
        {
            mCheckedListBox = new CheckedListBox();
            mCheckedListBox.CheckOnClick = true;
            mCheckedListBox.ItemCheck += new ItemCheckEventHandler(mCheckedListBox_ItemCheck);

            ToolStripControlHost mControlHost = new ToolStripControlHost(mCheckedListBox);
            mControlHost.Padding = Padding.Empty;
            mControlHost.Margin = Padding.Empty;
            mControlHost.AutoSize = false;

            mPopup = new ToolStripDropDown();
            mPopup.Padding = Padding.Empty;
            mPopup.Items.Add(mControlHost);
        }

        private void ReadFromXml()
        {

            if (!string.IsNullOrEmpty(xmlFile) && File.Exists(xmlFile))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(xmlFile);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int index = int.Parse(dr[0].ToString());
                    int displayIndex = int.Parse(dr[1].ToString());
                    bool visable = bool.Parse(dr[2].ToString());
                    mDataGridView.Columns[index].DisplayIndex = displayIndex;
                    mDataGridView.Columns[index].Visible = visable;
                }
            }
        }

        private void WriteToXml()
        {
            if (!string.IsNullOrEmpty(xmlFile))
            {
                DataTable dt = new DataTable("DataGrid");
                dt.Columns.Add("Index", typeof(Int32));
                dt.Columns.Add("DisplayIndex", typeof(Int32));
                dt.Columns.Add("Visable", typeof(bool));
                foreach (DataGridViewColumn col in mDataGridView.Columns)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = col.Index;
                    dr[1] = col.DisplayIndex;
                    dr[2] = col.Visible;
                    dt.Rows.Add(dr);
                }
                dt.WriteXml(xmlFile);
            }
        }

        void mDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.RowIndex == -1 && e.ColumnIndex == -1)
            {
                mCheckedListBox.Items.Clear();
                foreach (DataGridViewColumn c in mDataGridView.Columns)
                {
                    mCheckedListBox.Items.Add(c.HeaderText, c.Visible);
                }
                int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;
                mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? PreferredHeight : MaxHeight;
                mCheckedListBox.Width = this.Width;
                mPopup.Show(mDataGridView.PointToScreen(new Point(e.X, e.Y)));
            }
        }

        void mDataGridView_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
        {
            WriteToXml();
        }

        void mCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            mDataGridView.Columns[e.Index].Visible = (e.NewValue == CheckState.Checked);
            WriteToXml();
        }
    }

     项目中使用:      

   DataGridViewColumnSelector cs = new DataGridViewColumnSelector(dgvSession, DGVConfigPath);
   cs.Width = dgvSession.Width; 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值