get value from column using a column name string

35 pts.
Tags:
C#
Visual Studio 2010
Hi
I have 2 lists..
The first contains rows with mapping values inlcuding column name, xcord, ycord
The second contains the data I need to map.. 
I need to get the value in each row using the column name from the first row..
for example
List<SheetMappings> smaps = new <SheetMappings>();
Foreach(maplist m in mlist)
{
SheetMappings newMap = new SheetMappings();
foreach(vallist v in vlist)
{
newMap.Value = v.{m.ColumnName};
newMap.xCord = m.xCord;
newMap.yCord = m.yCord;
}
smaps.Add(newMap);
}
Any assitance appreciated
Cheers
Graham


Software/Hardware used:
visual studio 2010 c#

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 5  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    Please be a little more specific. What exactly do you need help with ? Does your code work (at least partially) ? Does it give errors ? Where is the mlist and vlist declaration ?
    69,510 pointsBadges:
    report
  • Ozvenison
    ok... what I am trying to do is get values from a table row to map the x,y cells of an excel sheet.. the first list contains the rows with Column_Name, yCord and yCord values (maplist) The second list contains rows with the actual values... the number of columns varies depending on which table I am processing... So, I loop through the 1st list and get a row from the spreadmaps,,, I then go through the second list and want to get the value from each row based on the column_name supplied from maplist... so v.'column_name'... ie v.date or v.openingBalance or v.closing balance.. where the column is based on what is supplied by the column_name string from spreadmaps I need to be 'dynamic' here as each sheet on the 'spread' control ca be based on a different table and therefore column_names Hope this makes sense..
    35 pointsBadges:
    report
  • Ozvenison
    at the moment I have to 'hard code' via a switch statement for each table and column names.. this if course will not work when the users add other tables... here is sample:
                List<SpreadMappings> spreadMapping = new List<SpreadMappings>();
                foreach (var m in mappings)
                {
                    foreach (var v in hvalues)
                    {
                        SpreadMappings map = new SpreadMappings();
                        value = v.GetType().GetProperty(m.ColumnName).GetValue(v, null);
                        switch (m.ColumnName)
                        {
                            case "DocHeading":
                                map.ColumnX = m.ColumnX;
                                map.ColumnY = m.ColumnY;
                                map.ColumnValue = v.DocHeading;
                                map.ColumnName = m.ColumnName;
                                map.ColumnId = v.Id;
                                map.ColumnSheetName = sheetName;
                                spreadMapping.Add(map);
                            break;
    
                            case "OrderCarryOverUnit1":
                                map.ColumnX = m.ColumnX;
                                map.ColumnY = m.ColumnY;
                                map.ColumnValue = v.OrderCarryOverUnit1;
                                map.ColumnName = m.ColumnName;
                                map.ColumnId = v.Id;
                                map.ColumnSheetName = sheetName;
                                spreadMapping.Add(map);
                            break;
    
    Cheers
    35 pointsBadges:
    report
  • Ozvenison
    the line value = v.GetType().GetProperty(m.ColumnName).GetValue(v, null); was were I was 'experimenting'
    35 pointsBadges:
    report
  • Ozvenison
    [...] 6. Add your insight into solving how to get a value from a column in Visual Studio. [...]
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following