Question

I have a datatable running in a foreach loop, getting site usage information on multiple sahrepoint websites. I would like to be able to add a column next to each foreach iteration adding the site url, I can only figure out how to do this adding a new row making the site url appear below the entry. Like So:

Data Table

How can I get the url to go into the row above it?

My code is below:

   SPListItemCollection items = list.GetItems(query);
                    DataTable aggregatedTable = new DataTable();
                    foreach (SPListItem item in items)
                    {
                        string url = item["SiteUrl"].ToString();
                        try
                        {
                            using (SPSite siteadd = new SPSite(url))
                            using (SPWeb webadd = siteadd.OpenWeb())
                            {
                                //


                                DataTable table = webadd.GetUsageData(Microsoft.SharePoint.Administration.SPUsageReportType.browser, Microsoft.SharePoint.Administration.SPUsagePeriodType.lastMonth);
                                table.Columns.Add("url");
                                if (table == null)
                                {
                                    //  HttpContext.Current.Response.Write("Table Null");
                                }
                                else
                                {

                                    DataRow dr;

                                    dr = table.NewRow();
                                    dr["url"] = url;
                                    table.Rows.Add(dr);

                                   // table.Rows.Add(url);



                                    aggregatedTable.Merge(table);//Append the data to previous site data.
                                }
                            }
                        }
                        catch { }
                    }
                    dataGridView1.DataSource = aggregatedTable;//bind datatable with 

No correct solution

OTHER TIPS

Why you adding a new row to you existing DataTable rather you should set value to you existing row.

e.g.

var CurRow = table.AsEnumerable().FirstOrDefault();
        table.Columns.Add("url");
        if (CurRow != null)
        {
            CurRow["url"] = url;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top