GridControl 選擇列、複選框全選,gridcontrol全選

來源:互聯網
上載者:User

GridControl 選擇列、複選框全選,gridcontrol全選

說明:

    GirdControl 中添加一列,這一列不是寫在資料庫中的,而是代碼中添加的。

圖示:

 

底層類代碼:

#region GridControl 全選        /// <summary>        /// 是否選中        /// </summary>        private static bool chkState = false;        //複選框列名稱        private static string chkFileName = "";        //複選框列寬        private static int chkWidth = 30;        //GridView         public static DevExpress.XtraGrid.Views.Grid.GridView GView = null;        private DevExpress.XtraGrid.Views.Grid.GridView gView {            get {                if (GView == null)                {                    GView = new DevExpress.XtraGrid.Views.Grid.GridView();                }                return GView;            }            set {                this.gView = value;            }        }         public static void GridCheckEdit(DevExpress.XtraGrid.Views.Grid.GridView gv, string checkFileName, int checkWidth)        {            if (gv != null) {                            chkFileName = checkFileName;            chkWidth = checkWidth;            GView = gv;            //不顯示複選框的欄位標題            gv.Columns[chkFileName].OptionsColumn.ShowCaption = false;                          //複選框的形狀   gv.Columns[chkFileName].ColumnEdit 執行個體是 repositoryItemCheckEdit1                          //repositoryItemCheckEdit1.CheckStyle = DevExpress.XtraEditors.Controls.CheckStyles.Standard;            ////複選框載入的狀態     實心   空心   空心打勾            //repositoryItemCheckEdit1.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;            //點擊事件            gv.Click += new System.EventHandler(gv_Click);            //畫列頭CheckEdit            gv.CustomDrawColumnHeader += new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler(gv_CustomDrawColumnHeader);            gv.DataSourceChanged += new EventHandler(gv_DataSourceChanged);            }        }        private static void gv_Click(object sender, EventArgs e)        {            if (ClickGridCheckBox(GView, chkFileName, chkState))            {                chkState = !chkState;            }        }        private static void gv_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)        {            if (e.Column != null && e.Column.FieldName ==chkFileName)            {                e.Info.InnerElements.Clear();                e.Painter.DrawObject(e.Info);                DrawCheckBox(e, chkState);                e.Handled = true;            }         }        private static void gv_DataSourceChanged(object sender, EventArgs e)        {            DevExpress.XtraGrid.Columns.GridColumn column = GView.Columns.ColumnByFieldName(chkFileName);            if (column != null)            {                column.Width = chkWidth;                column.OptionsColumn.ShowCaption = false;                column.ColumnEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();            }         }        private static void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)        {           DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit;            if (repositoryCheck != null)            {                System.Drawing.Graphics g = e.Graphics;                System.Drawing.Rectangle r = e.Bounds;                DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;                DevExpress.XtraEditors.Drawing.CheckEditPainter painter;                DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;                info = repositoryCheck.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;                painter = repositoryCheck.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;                info.EditValue = chk;                info.Bounds = r;                info.CalcViewInfo(g);                args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);                painter.Draw(args);                args.Cache.Dispose();            }        }        private static bool ClickGridCheckBox(DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus)        {            bool result = false;            if (gridView != null)            {                //禁止排序                 gridView.ClearSorting();                gridView.PostEditor();                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info;                System.Drawing.Point pt = gridView.GridControl.PointToClient(Control.MousePosition);                info = gridView.CalcHitInfo(pt);                if (info.InColumn && info.Column != null && info.Column.FieldName == fieldName)                {                    for (int i = 0; i < gridView.RowCount; i++)                    {                        gridView.SetRowCellValue(i, fieldName, !currentStatus);                    }                    return true;                }            }            return result;        }         #endregion

 

前台調用:

注意:GridControl綁定前,手動增加一列,增加完成之後,在綁定。

   //添加一列            dt.Columns.Add("chk", System.Type.GetType("System.Boolean"));            dt.Columns["chk"].DefaultValue = Boolean.FalseString;            gridControl1.DataSource = dt;            Functionjsj.GridCheckEdit(gv, "chk", 50);

chk 圖例:

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.