如何移动一行从一个DataGridView的另一个DataGridView的? [英] How do I move row from one DataGridView to another DataGridView?

查看:148
本文介绍了如何移动一行从一个DataGridView的另一个DataGridView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有相同的列架构2 DataGridViews(尽管两个不同的DataViews作为数据源 - 如果该事项)。什么是移动一行从一个datagridview的其他最好的/最快的方式?

I have two DataGridViews that have the same column schema (although two different DataViews as DataSource - if that matters). What is the best/quickest way to move a row from one datagridview to the other?

推荐答案

虽然我有两个数据表作为我DataGridViews结合,我无法看到周围使用的DataGridView获取所选行和移动它们,这是我的应用程序要求的任何方式。

Even though I have two DataTable as binding for my DataGridViews, I couldn't see any way around using the DataGridView to get the selected rows and move them, which is a requirement for my application.

GridUtility.cs

    /// <summary>
    /// Gets a list of the DataRow objects that are the datasources for a DataGridViews selected rows
    /// </summary>
    /// <param name="rows"></param>
    /// <returns></returns>
    public static DataRow[] GetSelectedDataRows(DataGridView grid)
    {   
        DataRow[] dRows = new DataRow[grid.SelectedRows.Count];
        for (int i = 0; i < grid.SelectedRows.Count; i++)
            dRows[i] = ((DataRowView)grid.SelectedRows[i].DataBoundItem).Row;

        return dRows;  
    }

    /// <summary>
    /// move row from one grid to another
    /// </summary>
    public void MoveRows(DataTable src, DataTable dest, DataRow[] rows)
    {
        foreach (DataRow row in rows)
        {   
            // add to dest
            dest.Rows.Add(row.ItemArray);

            // remove from src
            src.Rows.Remove(row);
        }
    }



要使用它:

GridUtility.MoveRows(fromTable, toTable, 
    GridUtility.GetSelectedDataRows(fromDataGridView));

这篇关于如何移动一行从一个DataGridView的另一个DataGridView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆