我如何在wpf c#中将dataGrid转换为数据表

时间:2022-09-02 13:32:41

I want to calculate total sum for a column, data showing in a DataGrid. but I have to convert Datagrid into data table to access or use that column. The data is coming from a rest-API built-in dot net core.

我想计算列的总和,数据显示在DataGrid中。但我必须将Datagrid转换为数据表才能访问或使用该列。数据来自rest-API内置点网核心。

How can I convert data grid into data table?

如何将数据网格转换为数据表?

XAML:

<DataGrid HorizontalAlignment="Left" Height="272" Margin="391,70,0,0" 
    VerticalAlignment="Top" Width="527" x:Name="GridCustomerSupplied" 
    SelectionChanged="GridCustomerSupplied_SelectionChanged"/>

Calling Method:

BindCustomerSuppliedGrid(GridCustomerSupplied,GetCurrentDate());

The error I get:

我得到的错误:

Unable to cast object of type 'System.Collections.Generic.List`1[MilkManagementUI.Models.ResponseModels.CustomerSuppliedResponseDto]' to type 'System.Data.DataView'.

无法将类型为'System.Collections.Generic.List`1 [MilkManagementUI.Models.ResponseModels.CustomerSuppliedResponseDto]'的对象强制转换为'System.Data.DataView'。

C#:

public static void BindCustomerSuppliedGrid(DataGrid grid,string date)
{
    try
    {
        var response = RestApiHelper<IEnumerable<CustomerSupplied>>.
            GetAll($"api/CustomerSupplied/all/date/{date}");

        grid.ItemsSource = response;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

1 个解决方案

#1


0  

using System.Data;

[...]

// Create DataTable
DataTable DataTable_Export = new DataTable();

// Instert DataGrid into DataTable
DataTable_Export = ((DataView)DataGrid_Input.ItemsSource).ToTable(); 

Thats all you Need to do.

这就是你需要做的一切。

#1


0  

using System.Data;

[...]

// Create DataTable
DataTable DataTable_Export = new DataTable();

// Instert DataGrid into DataTable
DataTable_Export = ((DataView)DataGrid_Input.ItemsSource).ToTable(); 

Thats all you Need to do.

这就是你需要做的一切。