How to change data in datatable in C #

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

The datatable data output 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 earlier to assign it to the Grid DataSource. I want to change the ReportName data of each row.

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

Can this be done? How can i do this?

I was thinking of doing 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.

+2


source to share


1 answer


your code will work fine, what is the problem? another option is to write an 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();

      

+1


source







All Articles