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; 


 

展开阅读全文

没有更多推荐了,返回首页