关于DevExpress Grid的Column中加checkbox的总结

1.Render checkBox:

 

        private bool m_checkStatus = false;

 

        [DebuggerStepThrough()]
        private void m_gridView_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && e.Column.FieldName == "Check")
            {
                e.Info.InnerElements.Clear();
                e.Painter.DrawObject(e.Info);
                DevControlHelper.DrawCheckBox(e, GetCheckStatus());
                e.Handled = true;
            }

        }

 

        [DebuggerStepThrough()]
        private bool GetCheckStatus()
        {
            return m_checkStatus;
        }

 

2.Process event:

 

        private void m_gridView_Click(object sender, EventArgs e)
        {
            bool chk = GetCheckStatus();
            if (DevControlHelper.ClickGridCheckBox(m_gridView, "Check", chk))
            {
                m_checkStatus = !chk;
            }
        }

 

 3.helper class method:

 

        [DebuggerStepThrough()]
        public static void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)
        {
            RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as RepositoryItemCheckEdit;
            if (repositoryCheck != null)
            {
                Graphics g = e.Graphics;
                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();
            }
        }

        public static bool ClickGridCheckBox(DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus)
        {
            bool result = false;
            if (gridView != null)
            {
                gridView.PostEditor();
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info;
                Point pt = gridView.GridControl.PointToClient(Control.MousePosition);
                info = gridView.CalcHitInfo(pt);
                if (info.InColumn && info.Column != null && info.Column.FieldName == fieldName)
                {
                    DataTable dt = gridView.GridControl.DataSource as DataTable;
                    if (dt != null)
                    {
                        bool chk = currentStatus;
                        foreach (DataRow dr in dt.Rows)
                        {
                            dr[fieldName] = false;
                        }

                        if (!chk)
                        {
                            // Only check filtered row
                            for (int i = 0; i < gridView.DataRowCount; i++)
                            {
                                DataRow row = gridView.GetDataRow(i);
                                if (row != null)
                                {
                                    row[fieldName] = true;
                                }
                            }
                        }
                        result = true;
                    }
                }
            }
            return result;
        }

 

 

你可能感兴趣的:(DevExpress)