I was facing this problem earlier today, and since I could not find a satisfactory solution, I decided to change my class design, and have seperate properties such as Tag 1, Tag 2, Tag 3 etc.
My main problem is the fact that I need to bind a grid to an object that contains a list among other properties and I need to show each item in the list as a separate column which I am unable to do. Hence I am resorting to declaring variables separately. Original question is here…
Now, I’m facing one of the most common design problem that probably every programmer has at some point of time. Here is the code to demonstrate it,
for (int i = 0; i < tags.Length; ++i) // Length not known here.
{
if(i==0){
tag1 = tags[0];
}
else if(i == 1){
tag2 = tags[1];
}
else if(i == 2){
tag3 = tags[2];
}
....
}
Here tags is a string array.
I was wondering if there is a more elegant way to do this. Another thing to note is that the efficiency of this loop decreases as it progresses, since with more iterations it has to check more conditions. If we could remove a condition after it had become true once it would speed up each iteration since we know that each condition will become true only once in all the iterations
Moved answer about
DataGridViewand usingComponentModelto the correct question:Displaying a list of object containing a list in a grid view
Briefing
The
DataGridViewcontroll supports theComponentModelnamespace so that you can create classes that appear to have properties that don’t exist. It is the same mechanism thePropertyGriduses.The sample code is in this answer of that question:
https://stackoverflow.com/a/13078735/195417
OLD ANSWER
This was my previous answer, when I didn’t realize the real question was about the
DataGridViewcontrol.Isn’t this the same as setting the values directly:
EDIT: as you sayd you don’t know how many variables will be needed, then you need only one, and that is a list:
If all you want is to copy all values, you can even do this:
Tell me whether this is what you want or not… maybe I have misunderstood the question.