Hello Friends....
Welcome to this Blog...This Blog is specially for the .Net Developers where all developers can discuss the .Net and Sql Articles.
"And will you succeed? Yes! You will indeed!(98 and 3/4 percent guaranteed)."

Wednesday, May 13, 2015

Export Grid View of .Net to Excel

Dear All,

Here is a new article for exporting your Grid View to Excel. Feel Free to ask your queries. Following are the steps for this:

1. Create a Grid View on Web Page.
2. Bind grid view with data.  For binding of gridview you can refer to the previous article "Dynamic Gridview Bind" Url: http://dinesh-itsolutions.blogspot.in/2010/09/dynamic-gridview-bind.html.

3. After binding the grid view put a button on the web page named "Export to Excel".  On Clicking of that button you can copy and paste the following code.

protected void btnExport(object sender, EventArgs e)
    Response.Buffer = true;
    Response.AddHeader("content-disposition",                                            "attachment;filename=ExportedFile.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //To Export all pages
        GridView1.AllowPaging = false;
        GridView1.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in GridView1.HeaderRow.Cells)
            cell.BackColor = GridView1.HeaderStyle.BackColor;
        foreach (GridViewRow row in GridView1.Rows)
            row.BackColor = Color.White;
            foreach (TableCell cell in row.Cells)
                if (row.RowIndex % 2 == 0)
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                    cell.BackColor = GridView1.RowStyle.BackColor;
                cell.CssClass = "textmode";
        string style = @"<style> .textmode { } </style>";
  //This is necessary event.So do copy/paste this also.
public override void VerifyRenderingInServerForm(Control control)

On Clicking of the button. You can successfully export your grid view to the excel.
Hope you like the article. :-)

Wednesday, September 7, 2011

Delete data from Gridview using Command Field

In this article, I am going to explain how to delete a record from Gridview.This is very easy process and steps for this is as follows:

1. Add New Webpage in your Webapplication.

2. Drag Gridview from toolbox and bind it to database.

Note:If you need any help in binding the gridview to the database you can view my article which is:

3. Now we proceed further after successfully binding of data.The Steps for Deleting records from gridview are:

a: Select Edit columns from properties of Gridview.

b:You will see dialog box appears for Field:

c:You have to select the Command Fields option from the available field.In Command Fields you will find 3 events which are:
  • Edit,Update and Cancel.
  • Select
  • Delete
From the above Points select Delete Button.

d: Now fire the event RowDeleteing from Gridview Properties

Coding to delete the data from gridview on Event Row Deleting is:

            protected void GridView1_RowDeleting(object sender,          
      GridViewDeleteEventArgs e)
           Label id =(Label)GridView1.
           SqlCommand save = new SqlCommand();
           save.Connection = con;
           save.CommandText="delete from tbtablename where id='"+     
           luck(); //bind function name

 If you any query in this article do ask me.

Thank You