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(); } }
|
| Author: macxima 03 Aug 2010 | Member 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 2010 | Member 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]); }
}
}
|