开发者

how to drag gridview row from one grid to another

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.

http://tv.devexpress.com/Content/XtraGrid/XtraGridDragRowsBetweenGrids/XtraGridDragRowsBetweenGrids.pdf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜