下拉树或者gridview 多选


        #region 列头 设置 复选框

        bool checkStatusBySelect = false;
        private void GridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            ColumnHeaderCustomDraw(e, checkStatusBySelect);
        }

        private void ColumnHeaderCustomDraw(ColumnHeaderCustomDrawEventArgs e, bool checkStatus)
        {
            if (e.Column != null && e.Column.FieldName == "Check")
            {
                //e.Column.Caption = " ";
                e.Info.InnerElements.Clear();
                e.Painter.DrawObject(e.Info);
                DevControlHelper.DrawCheckBox(e, checkStatus);
                e.Handled = true;
            }
        }

        private void ClickGridCheckBox(object sender, bool checkStatus, Point pt)
        {
            GridView gridView = sender as GridView;
            if (DevControlHelper.ClickGridCheckBox(gridView, "Check", checkStatus, pt))
            {
                checkStatusBySelect = !checkStatus;
            }
        }

        private void GridView_MouseDown(object sender, MouseEventArgs e)
        {
            System.Drawing.Point pt = new System.Drawing.Point(e.X, e.Y);
            ClickGridCheckBox(sender, checkStatusBySelect, pt);
        }
        #endregion

      public class DevControlHelper
        {
            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, Point pt)
            {
                bool result = false;
                if (gridView != null)
                {
                    // gridView.ClearSorting();//禁止排序

                    try
                    {
                        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)
                        {
                            info.Column.SortOrder = DevExpress.Data.ColumnSortOrder.None;
                            for (int i = 0; i < gridView.RowCount; i++)
                            {
                                gridView.SetRowCellValue(i, fieldName, !currentStatus);
                            }
                            return true;
                        }
                    }
                    catch { }
                    finally { }
                }
                return result;
            }

        }

分别需要:给 gridview添加两个事件:GridView_CustomDrawColumnHeader 和 GridView_MouseDown。上面方法和方法3

上面需要设置对应字段:check。此字段可变换。

 

2.不用字段:在gridview 属性里面

全部代码:


        public GroupTypeListForm(string strID):this()
        {
            _strID = strID;
        }

        string _strID = "";
        public DataRow[] _DataRow = null;
        GroupTypeDataSet.GroupTypeDataTable _DT = null;

        private void GroupTypeListForm_Load(object sender, EventArgs e)
        {
            using (Basic.GroupTypeWS.GroupTypeServiceClient c = new Basic.GroupTypeWS.GroupTypeServiceClient())
            {
                _DT = c.GetDataDT();
                _GroupTypebindingSource.DataSource = _DT;
            }

            this.gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(this.gridView1_FocusedRowChanged);
        }

        private void btChoice_Click(object sender, EventArgs e)
        {
            //获取行号
            int[] rownumber = this.gridView1.GetSelectedRows();
            _DataRow = new DataRow[gridView1.SelectedRowsCount];
            for (int i = 0; i < gridView1.SelectedRowsCount; i++)
            {
                int index = rownumber[i];
                _DataRow[i] = gridView1.GetDataRow(index);
            }
            this.DialogResult = DialogResult.OK;
        }
        

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (_strID != "")
            {
                string[] strid = _strID.Split(',');

                //传过来的ID和当前数据ID比较 获取到行号
                for (int j = 0; j < strid.Length; j++)
                {
                    for (int i = 0; i < _DT.Rows.Count; i++)
                    {
                        if (strid[j] == _DT.Rows[i]["ID"] + "")
                        {
                            this.gridView1.SelectRow(i);
                        }
                    }
                }
            }
            //只当一次性使用,注释事件
            this.gridView1.FocusedRowChanged -= new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(this.gridView1_FocusedRowChanged);
        }

赋值的新写法 帮勾选:使用 this.gridView1.DataRowCount 和 this.gridView1.GetDataRow(i);

 for (int i = 0; i < this.gridView1.DataRowCount; i++)
            {
                DataRow dr = this.gridView1.GetDataRow(i);
                string wxcd = dr["未下长度"] + "";
                if (wxcd != "" && Convert.ToInt32(wxcd) > 0)
                {
                    gridView1.SelectRow(i);
                }
            }

使用:

/// <summary>
        /// 选择可查看组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ribeGroupNameS_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var currRow = ((DataRowView)_UserBindingSource.Current).Row;

            string GroupIDS = currRow["GroupIDS"] + "";
            string GroupNameS = "";
            GroupTypeListForm frm = new GroupTypeListForm(GroupIDS);
            GroupIDS = "";
            if (frm.ShowDialog() == DialogResult.OK && frm._DataRow != null)
            {
                for (int i = 0; i < frm._DataRow.Length; i++)
                {
                    GroupIDS = GroupIDS == "" ? (frm._DataRow[i]["ID"] + "") : string.Format("{0},{1}", GroupIDS, frm._DataRow[i]["ID"]);
                    GroupNameS = GroupNameS == "" ? (frm._DataRow[i]["GroupName"]  + "") : string.Format("{0},{1}", GroupNameS, frm._DataRow[i]["GroupName"]);
                }
                currRow["GroupIDS"] = GroupIDS;
                currRow["GroupNameS"] = GroupNameS;
                currRow.EndEdit();
            }
            base.txtFocus.Focus();
        }

完成。一般选中方法二

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值