0

I have below class with one property.

  public class MfrYearEqpType
  {   
    public string EqpType { get; set; }

    public MfrYearEqpType()
    {
    }
  }

I wanted to create dynamic properties(Datatable column names) to my class "MfrYearEqpType" and set the values from below Datatable.

DataTable dt = getData();

This table contains 26 rows and 10 columns of data.

I have gone through the below link. But I am not sure how to handle my case.

Dynamically adding properties to an Object from an existing static object in C#

And also I have used ExpandoObject. I have done below sample.

  DataTable dt = getData();

  List<dynamic> expandoList = new List<dynamic>();

  foreach (DataRow row in dt.Rows)
  {
    //create a new ExpandoObject() at each row
   var expandoDict = new ExpandoObject() as IDictionary<String, Object>;
   foreach (DataColumn col in dt.Columns)
   {
      //put every column of this row into the new dictionary
      expandoDict.Add(col.ToString(), row[col.ColumnName].ToString());
   }

   //add this "row" to the list
   expandoList.Add(expandoDict);
   }

But my aim is to create the List of MfrYearEqpType. So that I can bind List of MfrYearEqpType to Gridview.

Please help me on this.

Naren
  • 65
  • 1
  • 2
  • 9
  • a) You can't decorate an existing class with new properties. You can only convert that object into something like an ExpandoObject. b) You have provided no code to demonstrate your problem, so we have nothing to assist with. Please provide a [mcve] for us to assist with. – ProgrammingLlama Jul 21 '17 at 00:24
  • Hi @john, I have edited the question. Please check. Thank you. – Naren Jul 21 '17 at 00:30
  • Change `col.ToString()` to `col.ColumnName`. Also, if you're using this to load data from a database and don't need to display it, use the SqlDataReader directly since using a DataTable will add unnecessary overhead. – ProgrammingLlama Jul 21 '17 at 00:32
  • Thank you @john. but I wanted to create List finally. Any clues? – Naren Jul 21 '17 at 00:40
  • That's not (easily) possible. You can't add new properties to an existing class. You could potentially dynamically generate a derived class (System.Reflection.Emit) with MfrYearEgpType as its parent, but you can't change an existing class. The link you provided simply converts an existing class into an ExpandoObject by copying its properties. If you know what will go into the class before compilation, you can declare the properties and use reflection to populate them. – ProgrammingLlama Jul 21 '17 at 00:45
  • 1
    Why can't you have the class with properties? – Chetan Jul 21 '17 at 01:09
  • Since you already have a datatable, why bother converting it to another type's collection? Simply bind that to GridView. – Tanveer Badar Apr 01 '18 at 15:38

1 Answers1

0

Assuming you are talking about ASP.NET here, you can directly bind your data table to a GridView's DataSource property.

If you are happy with these 10 columns displaying as default, you can set AutoGenerateColumns property to true. Otherwise, you may want to define <asp:BoundField /> or <asp:TemplateField /> columns as appropriate. Bound fields will let you at least format the value for a particular column while template fields will allow full customization where you can provide an defining how to render a particular field.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32