Few days back one gentleman asked me that how can I select a row within a GridView control using CheckBox while the auto-postback property of the CheckBox is set to true. Another gentleman asked me that how can he export the complete GridView to Excel sheet which has paging enabled. In the article I will going to address those two questions.

Introduction:

Few days back one gentleman asked me that how can I select a row within a GridView control using CheckBox while the auto-postback property of the CheckBox is set to true. Another gentleman asked me that how can he export the complete GridView to Excel sheet which has paging enabled. In the article I will going to address those two questions.

GridView with CheckBoxes Auto-PostBack = true:

Let's first see that how can we select the value of the GridView columns when we check the checkbox which is contained inside the GridView control. Here is the GridView HTML code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />

<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />

<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

As you can see I have two bound columns and one template column which contains the CheckBox. The AutoPostBack property of the CheckBox control is set to true which means the page will reload when you select a CheckBox.

When the CheckBox is clicked the CheckBox_CheckedChanged event is fired. We need to implement this method in order to catch the row in which the CheckBox is checked.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)

{

ClearCheckBoxes();

CheckBox checkbox = (CheckBox)sender;

checkbox.Checked = true;

GridViewRow row = (GridViewRow)checkbox.NamingContainer;

DisplayMessage(row.Cells[1].Text);

}

One thing I like to point out is that when you select a different CheckBox then your first selection is not cleared. In order to make your previous selections clear you can implement a simple ClearCheckBoxes method which will iterate through the GridView control and clear all the CheckBoxes.

private void ClearCheckBoxes()

{

foreach (GridViewRow row in GridView1.Rows)

{

CheckBox ch = (CheckBox) row.FindControl("CheckBox1");

ch.Checked = false;

}

}

As, you can see that in ClearCheckBoxes method I am simply clearing all the CheckBoxes inside the GridView control. The DisplayMessage method simply assigns the CategoryName to the Label's Text property since CategoryName belongs to Cell[1].

Exporting all Pages of the GridView to an Excel File: 

Now, to answer the seconds question. Well, this is pretty straight forward simply turn off the paging when you export the GridView and turn it on again after the exportation is completed. Here is the Button click code that is used to export the entire GridView (This will export the complete GridView data containing in all the pages) to Excel sheet.

Response.Clear();

        Response.AddHeader("content-disposition",
        "attachment;filename=FileName.xls");

        Response.Charset = "";

        
// If you want the option to open the Excel file without saving than

        // comment out the line below

        // 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);

        
// turn off paging 
        
GridView1.AllowPaging = false;
        BindData(); 
        

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();

        
// turn the paging on again 
        
GridView1.AllowPaging = true
;
        BindData();

I have made the important lines bold.

I hope you liked this article, happy coding!

If you are one of the thousands that visit GridViewGuy for your .NET articles and resources, you might be interested in making a donation. Extra cash helps pay for the hosting services and speed things up around here, and makes this website possible.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW