I have a web service and in the code below I loop through a collection of items. These items are desplayed in an xml like fashion (see example below). However I would like to display them in table like fashion. I thought of using the datatable to insert the items in there and then redisplay them.
585344
585344 Title Issue 1
585344 Number 140024
585344 State In progress
585350
585350 Title Issue 2
585350 Number 140026
585350 State Classification
How can I display them like this:
ID | Title | Number | State
585344 issue 1 140024 In progress
static void Main(string[] args)
{
ABWebService webSvc = new ABWebService();
GetObjectListData gold = new GetObjectListData();
gold.folderPath = "01. ITSM - Service Operation";
gold.RequiredField = new RequiredField[3];
gold.RequiredField[0] = new RequiredField {Value = "Title"};
gold.RequiredField[1] = new RequiredField {Value = "Number"};
gold.RequiredField[2] = new RequiredField {Value = "State"};
try
{
GetObjectListResult golr = webSvc.GetObjectList(gold);
List<NewsTracker> list = new List<NewsTracker>();
if (golr.success)
{
ObjectData[] myObjects = golr.Object;
for (int i = 0; i < myObjects.Length; i++)
{
Console.WriteLine(myObjects[i].id);
foreach (object myItem in myObjects[i].Items)
{
string field1 = string.Empty;
string val1 = string.Empty;
int val2 = 0;
string field2 = string.Empty;
StringVal item = myItem as StringVal;
if (item != null)
{
field1 = item.name;
val1 = item.Value;
Console.WriteLine("\t" + myObjects[i].id + " " + field1 + " " + val1);
}
LongIntVal val = myItem as LongIntVal;
if (val != null)
{
field2 = val.name;
val2 = val.Value;
Console.WriteLine("\t" + myObjects[i].id + " " + field2 + " " + val2);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
EDIT 1:
can you tell me how can I add them in a generic list like this: list.Add(new NewsTracker(title,number,state)); so that I can loop through and do other things with the list?
if you have only simple types then you can use reflection instead to build that table. here an example of a method i often use to send class info to a web service. Can be tweaked to receive a List and create 1 table with multiple rows once you understand the logic
public static DataTable ObjectToData(object o)
{
DataTable dt = new DataTable("OutputData");
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
o.GetType().GetProperties().ToList().ForEach(f =>
{
try
{
f.GetValue(o, null);
dt.Columns.Add(f.Name, f.PropertyType);
dt.Rows[0][f.Name] = f.GetValue(o, null);
}
catch { }
});
return dt;
}
If you pass the object ClassA per structure below
ClassA
{
string Value1 = "abc";
DateTime Value2 = DateTime.Now();
int Value3 = 12;
}
ObjectToData(MyClassA); datatable will look like the following :
|----------|-------------------------|----------|
| Value1 | Value2 | Value3 |
|==========|=========================|==========|
| "abc" | 2015/10/30 08:00:00 AM | 12 |
|----------|-------------------------|----------|
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With