Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

baskar
More...

Resources » Code Snippets » ASP.NET GridView

GridView


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 4


this is a snippet for exporting the contents to excel



 


this code deals with inserting a data to a database and displaying the items to the grid aas well and this code mainly focuses on how to export the items on the grid to excel,

here we deal with keywords like htmltextwriter and string writer, the string writer is mainly used for implementing htmltextwriter and the htmltextwriter is used for producing indented outputs and conversion purposes as well and the code is as follows

NOTE: set EnableEventValidation ="fase" at the top of your page



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public partial class Default6 : System.Web.UI.Page
{
string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Country values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(connection);
con.Open();
da = new SqlDataAdapter("Select * from Country", con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
public override void VerifyRenderingInServerForm(Control control)
{

}

private void ExportGridView()
{
string attachment = "attachment; filename=Country.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

}


protected void btnExcel_Click(object sender, EventArgs e)
{
GridView1.Columns[2].Visible = false;
GridView1.Columns[3].Visible = false;
ExportGridView();
}
private void clear()
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
btnSave.Enabled = true;
}
protected void btnClear_Click(object sender, EventArgs e)
{
this.clear();
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "GridView"
Author: macxima    03 Aug 2010Member Level: Gold   Points : 1
This is a simple example to export data into excel when gridview contains another controls like hyperlinks, buttons, checkboxes and radiobutton then what will you do?


Author: Shashwath77    03 Aug 2010Member Level: Gold   Points : 2
you are going to export only data so enable the controls to false or else i have don this as i used a check box

private void PrepareGridViewForExport(Control gv)
{
Literal ltCtrl = new Literal();
ImageButton imbbtn = new ImageButton();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{

if (gv.Controls[i].GetType() == typeof(CheckBox))
{
ltCtrl.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, ltCtrl);
}
else if (gv.Controls[i].GetType() == typeof(ImageButton))
{
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, ltCtrl);
}

if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}

}

}




Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Gridview no record found display empty grid(When Gridview add new row in footer)
    Previous Resource: Gridview control with full feature(Edit,Delete,Add New)
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Excel  .  
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.