Hi.
Here I would like to share a code to export gridview data to excel File. I hope it is useful to understand the concept behind on export grid view data.
When export gridview data you need to use one button to do the action of exporting.
We need to write the code on button event……
 

protected void Button1_Click(object sender, EventArgs e)
    {
        //***we need to set as paging as false to export all the data from the gridview.
        GridView1.AllowPaging = false;
        //***Again bind the data into gridview
        bindgrid();
        //***Function for to export the data...
        ExportExcel("File1.xls", GridView1);
            }


 Here the method for export excels method function………
  public static void ExportExcel(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();
               
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                 
                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                   table.Rows.Add(row);
                   
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                  
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

I hope you like this article to export the gridview data to excel file ..
Post your comment …

4:41 AM

Hope and Love

Posted by vijay


I would like to share my favourite on my reading book





Hope is not just another four letter word. It is perhaps, together with love, the most powerful emotion known to us. If Love makes the world go round. Hope keeps us going on forever. Even in the worst of times, it is Hope that keeps us alive. Hope make us believe in a better tomorrow and it is Hope that give courage to face all odds. As it is said Hope is lost, all is lost.

So when we are caught with vortex of thoughts such as :I am not good looking, so I cannot win her; I cannot get that job as I cannot do a good presentation; or I will never be able to complete my studies as I do not have finances, think of Hope.

Think of this quotation
It is difficult to say what is impossible, for the dream of yesterday is the hope of today and the reality of tomorrow.

12:21 AM

Boxing and UnBoxing in C#.Net and VB.NET

Posted by vijay


In Dot net they are two type of data types, namely Value type and Reference type. Already I have discuss these data types in an article for more refer the below link
http://www.dotnetcode.in/2012/12/data-types-in-c.html

Boxing:
The conversion of value type to reference type is known as Boxing. That means conversion of value type on stack to an object type on the heap.
Boxing is an implicit conversion type.
E.g.
C#
int m=50; (Value type)
object n; (Reference type)
n=m;
When we run the above code the compiler automatically convert m into reference type and place the value 50 in object n.
Vb.Net
Dim j as integer=50
Dim m as object =j
UnBoxing:
The conversion of reference type to value type is known as UnBoxing. That means conversion of reference type on heap to value type on stack.
But in unboxing the conversion takes place explicit operation using C-Type casting
E.g.
C#
Object m=10; (Refernce type)
Int n; (value type)
n=(int) m;
When unboxing a value, we must ensure that value type is large enough to hold the reference values otherwise it throw a runtime error for an example refers the below code...
object m=700;
byte n=(byte) m;
When you run the above code it will produce run time error, reason that byte type is not enough to hold the reference value.
Vb.Net
Dim m as object=10
Dim i as integer
i=ctype(m,integer)
I hope it helps. Post  your comments here