c# – 将数据从datagridview拖放到另一个

c# – 将数据从datagridview拖放到另一个,第1张

概述我有2个datagridviews,我想将datagridview1中的单元格复制到datagridview2(一次一个单元格).我能够选择我想要的单元格并将其拖动到datagridview2但是值没有显示… 我大部分时间都在寻找解决方案…可能是一个简单的答案,或者我只需要睡觉,但请帮助…. 我有以下代码 private void dataGridView1_MouseDown(object se 我有2个datagrIDvIEws,我想将datagrIDvIEw1中的单元格复制到datagrIDvIEw2(一次一个单元格).我能够选择我想要的单元格并将其拖动到datagrIDvIEw2但是值没有显示…
我大部分时间都在寻找解决方案…可能是一个简单的答案,或者我只需要睡觉,但请帮助….
我有以下代码
private voID dataGrIDVIEw1_MouseDown(object sender,MouseEventArgs e)    {        if (e.button == Mousebuttons.left)        {            DataGrIDVIEw.HitTestInfo info = dataGrIDVIEw1.HitTest(e.X,e.Y);            if (info.RowIndex >= 0)            {                if (info.RowIndex >= 0 && info.ColumnIndex >= 0)                {                    string text = (String)                           dataGrIDVIEw1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;                    if (text != null)                        dataGrIDVIEw1.DoDragDrop(text,DragDropEffects.copy);                }            }        }    }     private voID dataGrIDVIEw2_DragDrop(object sender,DragEventArgs e)    {        string cellvalue=e.Data.GetData(typeof(string)) as string;        Point cursorLocation=this.PointToClIEnt(new Point(e.X,e.Y));        System.windows.Forms.DataGrIDVIEw.HitTestInfo hittest= dataGrIDVIEw2.HitTest(cursorLocation.X,cursorLocation.Y);        if (hittest.ColumnIndex != -1            && hittest.RowIndex != -1)            dataGrIDVIEw2[hittest.ColumnIndex,hittest.RowIndex].Value = cellvalue;    }    private voID dataGrIDVIEw2_dragenter(object sender,DragEventArgs e)    {        e.Effect = DragDropEffects.copy;    }

而且我有设计师

// dataGrIDVIEw1        //         this.dataGrIDVIEw1.ColumnheadersHeightSizeMode = System.windows.Forms.DataGrIDVIEwColumnheadersHeightSizeMode.autoSize;        this.dataGrIDVIEw1.Location = new System.Drawing.Point(12,12);        this.dataGrIDVIEw1.name = "dataGrIDVIEw1";        this.dataGrIDVIEw1.Size = new System.Drawing.Size(299,150);        this.dataGrIDVIEw1.TabIndex = 0;        this.dataGrIDVIEw1.MouseDown += new System.windows.Forms.MouseEventHandler(this.dataGrIDVIEw1_MouseDown);        //         // dataGrIDVIEw2        //         this.dataGrIDVIEw2.AllowDrop = true;        this.dataGrIDVIEw2.ColumnheadersHeightSizeMode = System.windows.Forms.DataGrIDVIEwColumnheadersHeightSizeMode.autoSize;        this.dataGrIDVIEw2.Columns.AddRange(new System.windows.Forms.DataGrIDVIEwColumn[] {        this.Column1,this.Column2});        this.dataGrIDVIEw2.Location = new System.Drawing.Point(353,141);        this.dataGrIDVIEw2.name = "dataGrIDVIEw2";        this.dataGrIDVIEw2.Size = new System.Drawing.Size(240,150);        this.dataGrIDVIEw2.TabIndex = 5;        this.dataGrIDVIEw2.DragDrop += new System.windows.Forms.DragEventHandler(this.dataGrIDVIEw2_DragDrop);        this.dataGrIDVIEw2.dragenter += new System.windows.Forms.DragEventHandler(this.dataGrIDVIEw2_dragenter);        //
解决方法 您可以使用以下代码.我已经测试了它,它正在将单元数据从一个datagrIDvIEw复制到另一个datagrIDvIEw.
private voID dataGrIDVIEw2_dragenter(object sender,DragEventArgs e)    {        e.Effect = DragDropEffects.copy;    }    /* Drag & Drop */    private Rectangle dragBoxFromMouseDown;    private object valueFromMouseDown;    private voID dataGrIDVIEw1_MouseMove(object sender,MouseEventArgs e)    {        if ((e.button & Mousebuttons.left) == Mousebuttons.left)        {            // If the mouse moves outsIDe the rectangle,start the drag.            if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X,e.Y))            {                // Proceed with the drag and drop,passing in the List item.                                    DragDropEffects dropEffect = dataGrIDVIEw1.DoDragDrop(valueFromMouseDown,DragDropEffects.copy);            }        }    }    private voID dataGrIDVIEw1_MouseDown(object sender,MouseEventArgs e)    {        // Get the index of the item the mouse is below.        var hittestInfo = dataGrIDVIEw1.HitTest(e.X,e.Y);        if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)        {            valueFromMouseDown = dataGrIDVIEw1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex].Value;            if (valueFromMouseDown != null)            {                // Remember the point where the mouse down occurred.                 // The DragSize indicates the size that the mouse can move                 // before a drag event should be started.                                Size dragSize = Systeminformation.DragSize;                // Create a rectangle using the DragSize,with the mouse position being                // at the center of the rectangle.                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.WIDth / 2),e.Y - (dragSize.Height / 2)),dragSize);            }        }        else            // reset the rectangle if the mouse is not over an item in the ListBox.            dragBoxFromMouseDown = Rectangle.Empty;    }    private voID dataGrIDVIEw2_DragOver(object sender,DragEventArgs e)    {        e.Effect = DragDropEffects.copy;    }    private voID dataGrIDVIEw2_DragDrop(object sender,DragEventArgs e)    {        // The mouse locations are relative to the screen,so they must be         // converted to clIEnt coordinates.        Point clIEntPoint = dataGrIDVIEw2.PointToClIEnt(new Point(e.X,e.Y));        // If the drag operation was a copy then add the row to the other control.        if (e.Effect == DragDropEffects.copy)        {            string cellvalue = e.Data.GetData(typeof(string)) as string;            var hittest = dataGrIDVIEw2.HitTest(clIEntPoint.X,clIEntPoint.Y);            if (hittest.ColumnIndex != -1                && hittest.RowIndex != -1)                dataGrIDVIEw2[hittest.ColumnIndex,hittest.RowIndex].Value = cellvalue;        }    }
总结

以上是内存溢出为你收集整理的c# – 将数据从datagridview拖放到另一个全部内容,希望文章能够帮你解决c# – 将数据从datagridview拖放到另一个所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1247227.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存