0

I am converting a DataGridView to DataTable for some uses in WPF MVVM. Now, I have this code using the DataGridView with dgvSample as the DataGridView:

foreach(var transaction in transactionlist)
{
    dgvSample.Rows[0].Tag = transaction;
}

Since I am going to store the datagrid content in a DataTable, I have to do something like the following with dtSample as the datatable.

dtSample.Rows[0].Tag = transaction;

And I am having an error since tagging is not available in datatable rows. Is there any possible alternative of doing this?

Thank you!

ThEpRoGrAmMiNgNoOb
  • 1,256
  • 3
  • 23
  • 46
  • Instead of using a DataTable you could use a class and have the transaction as a property of the class. – Steve Sep 30 '19 at 08:16
  • https://stackoverflow.com/questions/3101412/how-to-extend-datarow-and-datatable-in-c-sharp-with-additional-properties-and-me – ASh Sep 30 '19 at 13:09

2 Answers2

1

Two possible solutions

  1. Add a column to the DataTable called Tag and store the value in that
  2. Create a DataSet, add the DataTable to the DataSet then create a new table Tags with a DataRelation to the original DataTable
Barracoder
  • 3,696
  • 2
  • 28
  • 31
1
  1. Add a "Tag" column to the DataTable:

    dtSample.Columns.Add(new DataColumn("Tag"));
    
  2. Set the value of the column like this:

    dtSample.Rows[0]["Tag"] = "...";
    
mm8
  • 163,881
  • 10
  • 57
  • 88