Translate This Blog

Grid view Short Points

************************Print & Export GridView in ASP.NET****************
STEP:1
public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }
STEP:2
 Change Page Directive Like
 Page Language="C#".....EnableEventValidation="false" ..

STEP:3
If Gridview in Update Panel then change triggers in update panel like

-Triggers- Open Triger
asp:PostBackTrigger ControlID="lnkbtnexportexcel"
asp:PostBackTrigger ControlID="imgbtnprint"
-/Triggers  Close triger

STEP:4 FOR Print Grid

protected void imgbtnprint_Click(object sender, ImageClickEventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "utf-8";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridViewAlerts.RenderControl(hw); //Pl_ToPrint
        Response.Write(sw.ToString());
        Response.End();

    }
***FOR EXPORT INTO Excel****
protected void lnkbtnexportexcel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridViewAlerts.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();

//---Create the File---------
        //Response.Buffer = true;
        //Response.ClearContent();
        //Response.ClearHeaders();
        //---For PDF uncomment the following lines----------
        //Response.ContentType = "application/pdf";
        //Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");

        //---For MS Excel uncomment the following lines----------
        //Response.ContentType = "application/vnd.ms-excel";
       //Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");

     //---For MS Word uncomment the following lines----------
        //Response.ContentType = "application/vnd.word";
        //Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");

        //---For CSV uncomment the following lines----------
        //Response.ContentType = "text/csv";
        //Response.AddHeader("content-disposition", "attachment;filename=FileName.csv");

        //---For TXT uncomment the following lines----------
        //Response.ContentType = "text/plain";
        //Response.AddHeader("content-disposition", "attachment;filename=FileName.txt");
        //EnableViewState = false;
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter hw = new HtmlTextWriter(sw);
        //Gridviewdashboard.RenderControl(hw);
        //Response.Write(sw.ToString());
        //Response.Flush();
        //Response.Close();
        //Response.End();      
    
    } 

---------------------------------------------------------------------------------------------------------------------------
Previous
Next Post »

1 comments:

Click here for comments
Anonymous
admin
December 27, 2013 at 2:07 AM ×

C# sort data in gridview

Congrats bro Anonymous you got PERTAMAX...! hehehehe...
Reply
avatar
Thanks for your comment