Question

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);
    }
    ...
}
Was it helpful?

Solution

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

Here is a similar post Convert DataTable to HTML Table

OTHER TIPS

Am a bit unsure if i have understood your requirement correctly but you can create a gridview dynamically from code using

Gridview myGrid = new GridView() 

as long as you have referenced the System.Web.UI namespace from that class.

Also, the GridView has a method

    public virtual void RenderControl(HtmlTextWriter writer)

which you can use to get a HTMLTextWriter with the Grids HTML which you can then pass on to your emailing class

From you edit - I assume you want to create html content or gridview in SendMail() method, right?

if so, you can opt the following method

  • Create an aspx page
  • put your gridview in it and bind it with datatable
  • Create httpWebrequest object and call newly created aspx page
  • read the content using HttpWebResponse
  • use this content to send mail.

here are some articles which will help you with httpwebrequest and httpwebresponse

Working with HttpWebRequest and HttpWebResponse in ASP.NET
HttpWebResponse Class
HttpWebRequest Class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top