在GridView中拖拉实现多选

在GridControl中的GridView属性栏中注册MouseDown、MouseMove以及MouseUp事件,在将timer计时器添加到GridControl中,并实现它的Tick事件。

另外:将GridView的OperationBehavior中的Editorable设为false(不可编辑),将OperationSelection中的MultiSelect设为true。

   #region   select many
        private int GetRowAt(GridView view, int x, int y)
        {
            return view.CalcHitInfo(new Point(x, y)).RowHandle;
        }

        //鼠标按下触发gdvPO_MouseDown事件
        private void gdvLotData_MouseDown(object sender, MouseEventArgs e)
        {
            StartRowHandle = GetRowAt(sender as GridView, e.X, e.Y);
        }

        //鼠标松开触发gdvPO_MouseDown事件
        private void gdvLotData_MouseUp(object sender, MouseEventArgs e)
        {
            StartRowHandle = -1;
            CurrentRowHandle = -1;
            timer1.Stop();
        }

        private void SelectRows(GridView view, int startRow, int endRow)
        {
            if (startRow > -1 && endRow > -1)
            {
                view.BeginSelection();
                view.ClearSelection();
                view.SelectRange(startRow, endRow);
                view.EndSelection();
            }
        }

        private void gdvLotData_MouseMove(object sender, MouseEventArgs e)
        {


            if (e.Button == MouseButtons.Left)
            {
                GridView view = sender as GridView;
                GridViewInfo info = view.GetViewInfo() as GridViewInfo;
                GridHitInfo hi = view.CalcHitInfo(e.Location);

                int newRowHandle = GetRowAt(sender as GridView, e.X, e.Y);
                if (CurrentRowHandle != newRowHandle)
                {
                    CurrentRowHandle = newRowHandle;
                    SelectRows(sender as GridView, StartRowHandle, CurrentRowHandle);
                }
                if (info.RowsInfo.Count < 1)
                {
                    return;
                }
                if (info.RowsInfo.Count >= 2 && info.RowsInfo[info.RowsInfo.Count - 2].RowHandle == hi.RowHandle)
                {
                    scrollDown = true;
                    if (!timer1.Enabled)
                        timer1.Start();
                }
                else
                {
                    if (info.RowsInfo[0].RowHandle == hi.RowHandle)
                    {
                        scrollDown = false;
                        if (!timer1.Enabled)
                            timer1.Start();
                    }
                    else
                        timer1.Stop();
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (scrollDown)
            {
                gdvLotData.TopRowIndex++;
            }
            else
            {
                gdvLotData.TopRowIndex--;
            }
            Point ee = gdcLotData.PointToClient(MousePosition);
            int newRowHandle = GetRowAt(gdvLotData, ee.X, ee.Y);
            if (CurrentRowHandle != newRowHandle)
            {
                CurrentRowHandle = newRowHandle;
                SelectRows(gdvLotData, StartRowHandle, CurrentRowHandle);
            }
        }

        #endregion

(子窗口)获取选中项的某列的值并用数组接收,在将该数组传入父窗口:

    private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                GlobalVariable.gaSelectPOID.Clear();
                int[] iaPOs = gdvLotData.GetSelectedRows();
                if (iaPOs.Length < 1)
                {
                    CommonFunction.ShowMsgBox("请选择一条或多条参数");//ToDo: Multi-Language
                    return;
                }
                int[] iaRAWID = new int[iaPOs.Length];//+2
                for (int i = 0; i < iaPOs.Length; i++)
                {
                    iaRAWID[i] = Convert.ToInt32(gdvLotData.GetRowCellValue(iaPOs[i], "RAW_ID"));
                }
                //saPARA_ID[iaPOs.Length] = cboModelVer.EditValue.ToString();
                //saPARA_ID[iaPOs.Length + 1] = "EVENT";

                if (iaRAWID.Length > 0)
                {
                    if (this.Owner is frmFDCTranMTSpecCalculate)
                    {
                        ((frmFDCTranMTSpecCalculate)this.Owner).LotDataRawIDs = iaRAWID;
                    }
                    this.Close();
                }

            }
            catch (Exception ex)
            {
                CommonFunction.ShowMsgBox("frmAPSReleaseWO.btnSelect_Click()\n" + ex.Message);
            }
        }

(父窗口)设置接收子窗口传来的数组:

 private int[] iaLotDataRawIDs;

        public int[] LotDataRawIDs
        {
            set
            {
                iaLotDataRawIDs = value;
            }
        }

验证是否成功接收(在父窗口中添加一个label和button),点击button将数组拼成字符串显示在label中:

 private void simpleButton1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < iaLotDataRawIDs.Length; i++)
            {
                this.labelControl13.Text += iaLotDataRawIDs[i].ToString() + ";";
            }
        }

=================

GridView设置表格不可编辑:

39f81393d3bd6b1c9f9bf765af1acb5aa38.jpg

 

 

转载于:https://my.oschina.net/8824/blog/3097164

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值