如何将gridview行从一个网格拖动到另一个网格 [英] how to drag gridview row from one grid to another
问题描述
任何有关这种拖放的好教程?感谢您的帮助。
更新:好的,我有以下代码(从gridPODetails拖到dataGridView1,它还没有工作,但我离我很近获取拖拽箭头和加号在目的地)。我缺少什么?
private void gridPODetails_MouseDown(object sender,MouseEventArgs e)
{
DataGridView.HitTestInfo info = gridPODetails.HitTest(eX,eY);
if(info.RowIndex> = 0)
{
// DataRowView view =(DataRowView)gridPODetails.Rows [info.RowIndex] .DataBoundItem; // WRONG
DataRow view =((DataTable)(gridPODetails.DataSource))。Rows [info.RowIndex]; // RIGHT
if(view!= null)
{
gridPODetails.DoDragDrop(view,DragDropEffects.Copy);
}
}
}
private void dataGridView1_DragEnter(object sender,DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView1_Dra gDrop(object sender,DragEventArgs e)
{
DataGridView grid = sender as DataGridView;
DataTable table = grid.DataSource as DataTable;
DataRow row = e.Data.GetData(typeof(DataRow))作为DataRow;
if(row!= null&&& table!= null&& row.Table!= table)
{
table.ImportRow(row);
row.Delete();
}
}
解决方案:查看我上面的编辑。我实际上抓住了整个datatable,而不仅仅是我想要的行。当然,目的地只知道如何使用Rows,而不是整个表。现在它的工作!
我发现这个 Bing search 。它看起来像你正在寻找的东西。
I have a gridview with rows. I'd like the user to be able to grab a row, and move it to one of several other gridviews on the winform. How can I go about doing this? I'm not familiar with how to implement drag and drop here.
Any good tutorials on this sort of drag and drop? Thanks for your help.
UPDATE: Ok, I have the following code (to drag from gridPODetails to dataGridView1. Its not working yet, but I"m closer (now I get drag arrow and plus sign in the destination). What am I missing?
private void gridPODetails_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo info = gridPODetails.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
//DataRowView view = (DataRowView)gridPODetails.Rows[info.RowIndex].DataBoundItem; //WRONG
DataRow view = ((DataTable)(gridPODetails.DataSource)).Rows[info.RowIndex]; //RIGHT
if (view != null)
{
gridPODetails.DoDragDrop(view, DragDropEffects.Copy);
}
}
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
DataGridView grid = sender as DataGridView;
DataTable table = grid.DataSource as DataTable;
DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
if (row != null && table != null && row.Table != table)
{
table.ImportRow(row);
row.Delete();
}
}
SOLVED: See my edit above. I was actually grabbing the entire datatable, not just the row I wanted. Of course the destination only knows how to work with Rows, not entire tables. Now its working!
I found this on a Bing search. It looks like exactly what you're looking for.
这篇关于如何将gridview行从一个网格拖动到另一个网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!