Search code examples
c#asp.netgridviewdynamic.class-file

Dynamically create a gridview from a class(.cs) file


I want to create a gridview form a class and send that gridview as mail. I know the mailing part but how to create a gridview from a class file without using an aspx file.

I am doing so because a method will be called at a scheduled time using quartz.net and that method will create a gridview and send it as mail. Any suggestion how it can be done?

EDIT:
This gridview contains daily works for staffs (ie) each staff has 'n' number of works. So i have dynamically generate a gridview inside a foreach loop. How it can be done?

using Quartz;
public class SendMailJob : IJob
{
    public void Execute(JobExecutionContext context)
    {
        SendMail();
    }
    private void SendMail()
    {
        // put your send mail logic here
    }
}

and global.asax,

using Quartz;
using Quartz.Impl;

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        ISchedulerFactory schedFact = new StdSchedulerFactory();
        // get a scheduler
        IScheduler sched = schedFact.GetScheduler();
        sched.Start();
        // construct job info
        JobDetail jobDetail = new JobDetail("mySendMailJob", typeof(SendMailJob));
        // fire every day at 06:00
        Trigger trigger = TriggerUtils.MakeDailyTrigger(06, 00);
        trigger.Name = "mySendMailTrigger";
        // schedule the job for execution
        sched.ScheduleJob(jobDetail, trigger);
    }
    ...
}

Solution

  • I would recommend creating an html table using StringBuilder rather than creating GridView.

    Here is a similar post Convert DataTable to HTML Table