Wpf 如何将DataGrid转换为dataTable

Wpf 如何将DataGrid转换为dataTable,wpf,vb.net,wpfdatagrid,wpf-4.0,Wpf,Vb.net,Wpfdatagrid,Wpf 4.0,我想将所有datagrid记录复制到datatable中,而不使用任何循环。 例如: 但这不起作用,并发出错误消息 我的开发平台是Visual studio 2010,语言是WPF和vb.net 4.0,这取决于数据网格的填充方式。如果DataContext属性设置为DataTable,则只需检索该值并转换为DataTable即可 没有直接方法将其从DataGrid元素转换为DataTable 如果要手动执行此操作,则必须创建DataTable的实例,然后使用循环从DataGrid中的项创建行。

我想将所有datagrid记录复制到datatable中,而不使用任何循环。 例如:

但这不起作用,并发出错误消息


我的开发平台是Visual studio 2010,语言是WPF和vb.net 4.0,这取决于数据网格的填充方式。如果DataContext属性设置为DataTable,则只需检索该值并转换为DataTable即可

没有直接方法将其从DataGrid元素转换为DataTable


如果要手动执行此操作,则必须创建DataTable的实例,然后使用循环从DataGrid中的项创建行。

必须在DataGrid上循环并将项添加到DataTable。下面的链接可能会有所帮助


我刚刚测试了这个,这个很有效

Dim dt as New DataTable
dt = ctype(Datagrid1.Datasource, DataTable)

这是在不使用循环的情况下将所有记录从DATAGRID传输到DATATABLE的方法

VB:

Dim dt As New DataTable
dt = CType(DataGrid1.ItemsSource, DataView).ToTable
DataTable dt = new DataTable();
dt = ((DataView)DataGrid1.ItemsSource).ToTable();  
C#:

Dim dt As New DataTable
dt = CType(DataGrid1.ItemsSource, DataView).ToTable
DataTable dt = new DataTable();
dt = ((DataView)DataGrid1.ItemsSource).ToTable();  

要将dataGrid转换为具有标题行的数据表,可以执行以下步骤:

1) 创建检索单元格的方法

  static public DataGridCell GetCell(DataGrid dg, int row, int column)
    {
        DataGridRow rowContainer = GetRow(dg, row);

        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }
这里有一个helper类来使用它

 /// <summary>
/// Class to help to retrive the Data Grid Contain
/// </summary>
public static class DataGridHelper
{
    /// <summary>
    /// Retrive the cell contains
    /// </summary>
    /// <param name="dg">DataGrid</param>
    /// <param name="row">row</param>
    /// <param name="column">column</param>
    /// <returns>DataGrid Cell content</returns>
    static public DataGridCell GetCell(DataGrid dg, int row, int column)
    {
        DataGridRow rowContainer = GetRow(dg, row);

        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }
    /// <summary>
    /// Get row content
    /// </summary>
    /// <param name="dg">Datagrid</param>
    /// <param name="index">Index</param>
    /// <returns>DataGridRow</returns>
    static public DataGridRow GetRow(DataGrid dg, int index)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // may be virtualized, bring into view and try again
            dg.ScrollIntoView(dg.Items[index]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
    static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
}
//
///类以帮助检索包含的数据网格
/// 
公共静态类DataGridHelper
{
/// 
///检索单元格包含的内容
/// 
///数据网格
///划船
///纵队
///数据网格单元内容
静态公共DataGridCell GetCell(DataGrid dg,int行,int列)
{
DataGridRow rowContainer=GetRow(dg,row);
if(rowContainer!=null)
{
DataGridCellsPresenter=GetVisualChild(行容器);
//尝试获取该单元,但它可能已虚拟化
DataGridCell=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
if(单元格==null)
{
//现在试着把手机放在视野里,然后拿回手机
dg.ScrollIntoView(行容器,dg.Columns[列]);
单元格=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
}
返回单元;
}
返回null;
}
/// 
///获取行内容
/// 
///数据网格
///索引
///DataGridRow
静态公共DataGridRow GetRow(DataGrid dg,int索引)
{
DataGridRow行=(DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(索引);
if(行==null)
{
//可能已虚拟化,请查看并重试
总卷轴视图(总项目[索引]);
行=(DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(索引);
}
返回行;
}
静态T GetVisualChild(可视父级),其中T:Visual
{
T child=默认值(T);
int numVisuals=VisualTreeHelper.GetChildrenCount(父级);
对于(int i=0;i
我在C#DataGrid上找不到“ItemSource”,如何使用mvvmI可以很好地实现这一点,但我不得不添加自己的GetVisualChild和GetRow方法,因为它们没有发布在这里。很抱歉,忘记了,但正如您看到的,创建自己的方法来检索row和GetVisualChild很容易。让我知道如果你想我发布了这个方法,你可以添加这些方法,使答案完整吗?嗨,杰里米,我添加了助手类,我希望这能帮助你
 /// <summary>
/// Class to help to retrive the Data Grid Contain
/// </summary>
public static class DataGridHelper
{
    /// <summary>
    /// Retrive the cell contains
    /// </summary>
    /// <param name="dg">DataGrid</param>
    /// <param name="row">row</param>
    /// <param name="column">column</param>
    /// <returns>DataGrid Cell content</returns>
    static public DataGridCell GetCell(DataGrid dg, int row, int column)
    {
        DataGridRow rowContainer = GetRow(dg, row);

        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }
    /// <summary>
    /// Get row content
    /// </summary>
    /// <param name="dg">Datagrid</param>
    /// <param name="index">Index</param>
    /// <returns>DataGridRow</returns>
    static public DataGridRow GetRow(DataGrid dg, int index)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // may be virtualized, bring into view and try again
            dg.ScrollIntoView(dg.Items[index]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
    static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
}