0

I have a class named Agent:

public class Agent
{
    public string Measure { get; set; }
}

I have a Datatable, whose columns are dynamic (different every time from database except "Measure" column)

For Example:

Measure    |     Dexter     |     Jordon     |     Ana     |     Polark     |
Login hour        8.7              5.5             7.5            4.8

After 10 seconds, the Datatable will have:

Measure    |     Robert     |      Leo       |    Black    |     Operah     |
Login hour        9.6              5.5             4.3            4.8

When the datatable is created. I want to add dynamic properties to the class "Agent" at "Runtime" each time. It should become:

public class Agent
{
    public string Measure { get; set; }
    public string Dexter { get; set; }
    public string Jordon { get; set; }
    public string Ana { get; set; }
    public string Polark { get; set; }
}

After it is created, I want to Give the class "Agent" to the MVC View as its model. How I can achieve this.

Note: I am a beginner, so please help me out. I can,t find the solution anywhere.

1
  • Wh4t have you tr1ed? Are you asking how to get the data out or how to pass it to the view? Commented Apr 2, 2014 at 11:33

2 Answers 2

1

I don`t think you can add things that way dynamically, but perhaps you can use a dictionary instead:

public class Agent
{
    public string Measure { get; set; }
    public Dictionary<string, string> Scores {get; set;}    
}

Then you can add and get stuff from it like:

instanceOfAgent.Scores.Add("Rob", "9.6");
instanceOfAgent.Scores.Add("John", "4.8");
...

var johnsScore = instanceOfAgent.Scores["John"]; // will return "4.8"
Sign up to request clarification or add additional context in comments.

Comments

0

This is not a good way to solve the problem.

Your Agent class should have a string property called Name that holds the name of the Agent (e.g. "Dexter", "Jordon" etc) If your Measure property contains a value like "Logins/hour" then you probably need a Value property that contains values such as "9.6", "5.5" etc.

One Agent object then holds the Name, Measure and Value for one Agent.

Your Agent DataTable is then populated with Agent records from the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.