dev gridcontrol (二)绑定值控件拖动排序功能,序号

因为要做一个排序功能,弄了很久自带的功能都没法弄出来,最后自己写了一个方法来实现

绑定数据的时候用泛型,拖动的时候获到到选中的行和要上目行上的行号,再使用下面的方法

 

注册事件

 

 protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //添加事件
            this.gridControl1.DragOver += new System.Windows.Forms.DragEventHandler(this.gridControl1_DragOver);
            this.gridControl1.DragDrop += new System.Windows.Forms.DragEventHandler(this.gridControl1_DragDrop);
            this.gridControl1.MouseDown += new MouseEventHandler(gridControl1_MouseDown);
            this.gridControl1.MouseMove += new MouseEventHandler(gridControl1_MouseMove);
            gridControl1.AllowDrop = true;
            BindGrid(null);

        }

 

 

 

 绑定值

 /// <summary>
        
/// 绑定数据
        
/// </summary>
        
/// <param name="modelList"></param>
        private void BindGrid(List<OrderModel> modelList)
        {
            if (modelList == null)
            {
                modelList = new List<OrderModel>();
                string sqlStr = "select functionid, functiontitle,loadorder from evbase_functionlist where parentid=" + ParentID + " order by loadorder asc";
                DataTable dt = mNSCEVDBManager.GetDataTable(sqlStr, null);
                foreach (DataRow dr in dt.Rows)
                {
                    OrderModel model = new OrderModel() { ID = dr["FUNCTIONID"].ToString(), Title = dr["FUNCTIONTITLE"].ToString(), LoadOrder = dr["LOADORDER"].ToString() };
                    modelList.Add(model);
                }

            }
            this.gridControl1.DataSource = modelList;
             this.gridControl1.RefreshDataSource();
        }

 

 

拖动的事件

 

  #region
        //<gridControl1>
        private void gridControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            hitInfo = gridView1.CalcHitInfo(new Point(e.X, e.Y));
            if (hitInfo.RowHandle < 0) hitInfo = null;
        }

        private void gridControl1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (hitInfo == nullreturn;
            if (e.Button != MouseButtons.Left) return;
            Rectangle dragRect = new Rectangle(new Point(
                hitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2,
                hitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2), SystemInformation.DragSize);
            if (!dragRect.Contains(new Point(e.X, e.Y)))
            {
                if (hitInfo.InRow)
                {
                    gridControl1.DoDragDrop(new object[] { hitInfo.RowHandle, gridControl1 }, DragDropEffects.All);
                }
            }
        }
        //</gridControl1>

        private int GetDragObject(object sender, IDataObject data)
        {
            object[] obj = data.GetData(typeof(object[])) as object[];
            if (obj == null || !sender.Equals(gridControl1)) return -2;
            if (obj[0is Int32)
                return (int)obj[0];
            else return -1;
        }

        //<gridControl1>
        private void gridControl1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
        {
            GridHitInfo hi = gridView1.CalcHitInfo(gridControl1.PointToClient(new Point(e.X, e.Y)));
            int handle = GetDragObject(sender, e.Data);
            if (hi.InRow && handle >= 0 && handle != hi.RowHandle)
            {
                if ((e.KeyState & 8) != 0)
                    e.Effect = DragDropEffects.Copy;
                else
                    e.Effect = DragDropEffects.Move;
            }
            else e.Effect = DragDropEffects.None;
        }

        private void gridControl1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            var tempList = gridView1.DataSource as List<OrderModel>;
            int currentID = GetDragObject(sender, e.Data);

            GridHitInfo hi = gridView1.CalcHitInfo(gridControl1.PointToClient(new Point(e.X, e.Y)));
            int newHandle = hi.RowHandle;
            // int result = currentID - newHandle;
            OrderModel model = tempList[currentID];

            tempList.RemoveAt(currentID);
            //  tempList.Add(model);
            tempList.Insert(newHandle, model);
            BindGrid(tempList);
        }
        //</gridControl1>

        private void gridView1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!MouseButtons.Equals(MouseButtons.None))
                e.Cancel = true;
        }
        #endregion

 

 

 显示序号

 

void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

 

 

posted @ 2012-05-28 17:11  咸鱼公子  Views(2366)  Comments(0Edit  收藏  举报