Drag and Drop to Realize Multiple Selections in GridView

Posted by many_pets on Wed, 02 Oct 2019 23:18:34 +0200

Register MouseDown, MouseMove and MouseUp events in the GridView property bar of GridControl, add timer timer to GridControl, and implement its Tick event.

Additionally: Set Editorable in GridView OperationBehavior to false (non-editable) and MultiSelect in OperationSelection to true.

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

        //Click on the mouse to trigger the gdvPO_MouseDown event
        private void gdvLotData_MouseDown(object sender, MouseEventArgs e)
        {
            StartRowHandle = GetRowAt(sender as GridView, e.X, e.Y);
        }

        //Mouse release triggers gdvPO_MouseDown event
        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

(child window) Gets the value of a column of the selected item and receives it in an array, which is passed into the parent window:

    private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                GlobalVariable.gaSelectPOID.Clear();
                int[] iaPOs = gdvLotData.GetSelectedRows();
                if (iaPOs.Length < 1)
                {
                    CommonFunction.ShowMsgBox("Please select one or more parameters");//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);
            }
        }

(parent window) Sets the array that receives the incoming child window:

 private int[] iaLotDataRawIDs;

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

Verify successful reception (add a label and button to the parent window). Click button to spell the array into strings and display them in label:

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

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

GridView Settings Table Not Editable:

Topics: Programming