tags:

views:

472

answers:

1

Hi,

I'm using Visual Studio 2008 (Asp.net (c#)) and SQl Server 2005 I have a datatable ("dtReportList") generated from database query and then I show the datatable by a gridview.
dtReportList = GetReportList(UserId); //get data from database
GridView1.DataSource = dtReportList;
GridView1.DataBind();

The output data of datatable will be::

ReportId    ReportName      CreatedDate
1           DummyReport1    21/08/2009
2           DummyReport2    21/08/2009
3           DummyReport4    21/08/2009

I want to modify the DataTable before to assign it to the Grid's DataSource. I want to modify the ReportName data of each row.

I cannot modifed the GetReportList method because is heavily used, so my only chance is to modify the DataTable before I will use it.

Is it possible to do it? How can I do it?

I was thinking to do something like this?

dtReportList = GetReportList(UserId); //get data from database  
foreach(DataRow report in dtReportList.Rows)  
{  
    Guid reportId = new Guid(report["ReportId"].ToString());  
    string reportTitle = GetReportTitle(conn, reportId, UserId,
        GetReportLanguage(reportId));  
    report["ReportName"] = reportTitle;  
}  
GridView1.DataSource = dtReportList;  
GridView1.DataBind();

Thanks again for your help.

A: 

your code will work normally , what is the problem? the other choice is to write extension method and use it when needed, something like this

public static class ReportsExtensions
{

    public static GetChangedList(this ReportsTable tbl)
    {
        foreach(DataRow report in tbl)  
        {  
             Guid reportId = new Guid(report["ReportId"].ToString());  
             string reportTitle = GetReportTitle(conn, reportId, UserId, GetReportLanguage(reportId));  
             report["ReportName"] = reportTitle;  
        }  
    }
}
GridView1.DataSource = dtReportList.GetChangedList();  
GridView1.DataBind();
ArsenMkrt
Thanks.I was not getting the changes made in the DataTable (maybe I didn't compile properly the code).I like your code more because I will be able to use it everywhere if needed.
SergioKastro