CareTaker22
CareTaker22

Reputation: 1300

Converting DataGrid.ItemSource to DataTable

I am in need of converting my current DataGrid's Item Soucrce with all of it's contents to a new DataTable.

Here is my coding of where I insert all of my information into the dgFeedbackSelectSupplier's ItemSource

private void btnFeedbackSelectSupplier_Click(object sender, RoutedEventArgs e)
{
    DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn();
    columnFeedbackSupplier.Header = "Supplier";
    columnFeedbackSupplier.CanUserReorder = true;
    columnFeedbackSupplier.CanUserResize = true;
    columnFeedbackSupplier.IsReadOnly = false;

    var stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

    DataTemplate cellTemplate = new DataTemplate();

    FrameworkElementFactory factoryCheck = new FrameworkElementFactory(typeof(CheckBox));
    Binding bindCheck = new Binding("TrueFalse");
    bindCheck.Mode = BindingMode.TwoWay;
    factoryCheck.SetValue(CheckBox.IsCheckedProperty, bindCheck);
    stackPanel.AppendChild(factoryCheck);

    FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox));
    Binding bindText = new Binding("Supplier");
    bindText.Mode = BindingMode.TwoWay;
    factoryText.SetValue(TextBox.TextProperty, bindText);
    stackPanel.AppendChild(factoryText);

    cellTemplate.VisualTree = stackPanel;
    columnFeedbackSupplier.CellTemplate = cellTemplate;

    DataGridTextColumn columnFeedbackSupplierItem = new DataGridTextColumn();
    columnFeedbackSupplier.Header = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Name;

    dgFeedbackAddCost.SelectAll();

    IList list = dgFeedbackAddCost.SelectedItems as IList;
    IEnumerable<ViewQuoteItemList> items = list.Cast<ViewQuoteItemList>();

    var collection = (from i in items
                      let a = new ViewQuoteItemList { Item = i.Item, Supplier = 25, TrueFalse = false } //Remove Supplier(it's for test only)
                      select a).ToList();

    dgFeedbackSelectSupplier.Columns.Add(columnFeedbackSupplier);
    dgFeedbackSelectSupplier.ItemsSource = collection;

    lblFeedbackRemoveCompany.Content = collection.ToList().Sum(x => x.Supplier);
}

In my next event, I'm trying to convert that ItemSource to a new DataTable like this:

    private void btnFeedbackGetTotals_Click(object sender, RoutedEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = ((DataView)dgFeedbackSelectSupplier.ItemsSource).ToTable(); //Error here
        var sum = 0;
        foreach (DataRow dr in dt.Rows)
        {
            foreach (DataColumn dc in dt.Columns)
            {
                sum += (int)dr[dc];
            }
        }
    }

But i'm receiving the following error when I run the above method:

Unable to cast object of type 'System.Collections.Generic.List`1[MKCWorkflowApplication.ViewQuoteItemList]' to type 'System.Data.DataView'.

I have no clue why it's doing this, as I thought that any datagrid's item source could simply be converted to a new datatable. Is there a way to fix this?

EDIT:

My class 'ViewQuoteItemList'

public class ViewQuoteItemList
{
    public string CustomerRFQ { get; set; }
    public int QuoteId { get; set; }
    public int Id { get; set; }
    public string Item { get; set; }
    public string Material { get; set; }
    public string Description { get; set; }
    public string AdditionalInformation { get; set; }
    public int Quantity { get; set; }
    public string Cost { get; set; }
    public decimal Supplier { get; set; }
    public bool TrueFalse { get; set; }

    public string CheckBoxColumn
    {
        get { return string.Format("{0} {1}", Supplier, TrueFalse); }
    }
}

Upvotes: 2

Views: 589

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460038

Why do you want to convert a strongly typed List<ViewQuoteItemList> to a loosely typed DataTable at all? You don't need the DataTable, you can use following LINQ query.

var source = (List<ViewQuoteItemList>) dgFeedbackSelectSupplier.ItemsSource;
int sum = source.Sum(x => x.Prop1 + x.Prop2 + x.Prop3); // change accordingly

If that doesn't help you should show us the class ViewQuoteItemList.

Upvotes: 2

Related Questions