private void export_to_word()
    {

        try
        {
            data_grid_print.Visible = true;
            //grd_detail.Visible = true;
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.word";

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            data_grid_print.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());

            Response.End();
            htmlWrite.Flush();
            //grd_detail.Visible = false;
            data_grid_print.Visible = false;
        }
        catch (Exception ex)
        {
        }
    }
 private void export_to_pdf()
    {
        try
        {

            data_grid_print.Visible = true;
            HtmlForm form = new HtmlForm();
            form.Controls.Add(data_grid_print);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
            form.Controls[0].RenderControl(hTextWriter);
            string html = sw.ToString();
            Document Doc = new Document();
            PdfWriter.GetInstance
            (Doc, new FileStream(Environment.GetFolderPath
            (Environment.SpecialFolder.Desktop)
            + "\\Pin_report_unsoldpin.pdf", FileMode.Create));
            Doc.Open();
            Chunk c = new Chunk
            ("PDF Report For Unsoldpin  \n",
            FontFactory.GetFont("Verdana", 10));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            Chunk chunk1 = new Chunk
            (" \n", FontFactory.GetFont("Verdana", 6));
            Paragraph p1 = new Paragraph();
            p1.Alignment = Element.ALIGN_RIGHT;
            p1.Add(chunk1);

            Doc.Add(p);
            Doc.Add(p1);
            System.Xml.XmlTextReader xmlReader =
            new System.Xml.XmlTextReader(new StringReader(html));
            HtmlParser.Parse(Doc, xmlReader);

            Doc.Close();
            string Path = Environment.GetFolderPath
            (Environment.SpecialFolder.Desktop)
            + "\\Pin_report_unsoldpin.pdf";


            //ShowPdf(Path);
            data_grid_print.Visible = false;

        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }
private void export_to_excel()
    {
        try
        {
            //grd_detail.Visible = true;
            data_grid_print.Visible = true;
            Response.Clear();

            Response.AddHeader("content-disposition", "attachment;filename=Customerlist.xls");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            data_grid_print.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            data_grid_print.Visible = false;
            //grd_detail.Visible = false;
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }
                              
     SmtpClient smtpclient;
        MailMessage message;
            string str234 = "Name=" + txtname.Text + "\n Address=" + txtaddress.Text 
            + "" + "'\n Phone No=" + txtmobileno.Text + "\n Fax No=" + txtfax.Text
            + "\n Email=" + txtemail.Text + "\n Message=" + txtmessage.Text;

            smtpclient = new SmtpClient();
            message = new MailMessage();

            message.From = new MailAddress("developerlifestyle@gmail.com");
            message.To.Add("developerlifestyle@gmail.com");  //send email to gmail

            message.Subject = "Contact Form";
            message.Body = str234;
            smtpclient.Host = "smtp.gmail.com";
            smtpclient.EnableSsl = true;
            smtpclient.UseDefaultCredentials = true;
            System.Net.NetworkCredential network = new System.Net.NetworkCredential();
            network.UserName = "developerlifestyle@gmail.com"; 
            network.Password = "*******"; //password
            smtpclient.UseDefaultCredentials = true;
            smtpclient.Credentials = network;
            smtpclient.Port = 25;
            smtpclient.Send(message);

Gridview in Asp.Net displays the data in tabular format using rows and columns. Sometimes you may have requirement to group or merge same rows based on data.

For example you are displaying the country data by using Gridview. Grid has CountryId, CountryName and State data. We have requirement to display the country states as group. To merge the Gridview rows we have to use Gridview PreRender event to compare and merge the related rows as shown below.

using System; 
using System.Data; 
using System.Web.UI.WebControls;

namespace GridviewMergeRows 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
                CreateTable(); 
        }

        private void CreateTable() 
        { 
            try 
            { 
                DataTable dt = new DataTable(); 
                dt.Columns.Add("CountryId"); 
                dt.Columns.Add("CountryName"); 
                dt.Columns.Add("State"); 

                dt.Rows.Add("1", "US", "Alabama"); 
                dt.Rows.Add("1", "US", "Hawaii"); 
                dt.Rows.Add("1", "US", "Georgia"); 
                dt.Rows.Add("1", "US", "Alaska"); 
                dt.Rows.Add("2", "UK", "United Kingdom"); 
                dt.Rows.Add("3", "India", "Delhi"); 
                dt.Rows.Add("3", "India", "Andhra Pradesh"); 
                dt.Rows.Add("3", "India", "Maharashtra");

                gv1.DataSource = dt; 
                gv1.DataBind(); 
            } 
            catch (Exception ex) 
            { } 
        } 

        private void MergeGridviewRows(GridView gridView) 
        { 
            for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--) 
            { 
                GridViewRow row = gridView.Rows[rowIndex]; 
                GridViewRow previousRow = gridView.Rows[rowIndex + 1]; 

                if (row.Cells[0].Text == previousRow.Cells[0].Text && row.Cells[1].Text == previousRow.Cells[1].Text) 
                { 
                    row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 : previousRow.Cells[0].RowSpan + 1; 
                    row.Cells[1].RowSpan = previousRow.Cells[1].RowSpan < 2 ? 2 : previousRow.Cells[1].RowSpan + 1; 

                    previousRow.Cells[0].Visible = false; 
                    previousRow.Cells[1].Visible = false; 
                } 
            } 
        } 

        protected void gv1_PreRender(object sender, EventArgs e) 
        { 
            MergeGridviewRows(gv1); 
        }
     }
} 

As shown above, we are merging the Gridview rows in MergeGridviewRows method by comparing CountryId and CountryName fields. We are calling this MergeGridviewRows method in Gridview PreRender event. 
After merging the related Gridview rows, the output displays as shown below.