GridView Select Multiple Rows using C# ASP.Net AJAX

by top54u.com 09 May, 2008
Spotlight.....

Introduction

In ASP.Net 2.0 you can use CommandField with Select command of GridView to select a row. But little logic is required to select multiple rows of GridView control. You can use AJAX server controls like UpdatePanel to perform the task with asynchronous postbacks that also improves the performance of the web application by decreasing the page load time factor on every page refresh.

 

GridView Select Multiple Rows

To maintain the state of GridView control so that it displays the selected GridViewRows with different style you have to store the unique id of selected row in any type of Collection. Here we will use DataTable to store the values and its PrimaryKey constraint will help in storing the unique values in each rows.

Further, for the functionality to select and unselect the GridViewRows PrimaryKey constraint on DataTable column enables you to remove the selected row from the DataTable and unselect the corresponding GridViewRow.

SelectedIndexChanging commandfield event of GridView Control retrieves the NewSelectedIndex of GridViewRow that can be inserted into the DataTable.

 

C# Code to Select Multiple GridView Rows using AJAX

Categories Table of Northwind Database is used to provide the free C# sample code for GridView Multiple Row Select.

In GridView Control categoryid is set as DataKeyNames to retrieve the unique id of categories.

HTML code for GridView Template:

<asp:GridView ID="grdCategories" runat="server" AutoGenerateColumns="False" Width="200px" DataKeyNames="categoryid" OnSelectedIndexChanging="grdCategories_SelectedIndexChanging">
                    <Columns>
                    <asp:CommandField HeaderText="Select/Unselect" SelectText="select / unselect" ShowHeader="True" ShowSelectButton="True" />
                    <asp:BoundField DataField="categoryName" HeaderText="Category Name" />
                    </Columns>
</asp:GridView>

 

C# Code to handle SelectedIndexChanging

    protected void grdCategories_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        saveMultiSelect( Convert.ToInt32(grdCategories.DataKeys[ e.NewSelectedIndex ][ "categoryid" ].ToString()));
    }

 

In the above C# code saveMultiSelect function performs the task to select / unselect the multiple rows of gridview control. It saves the values in DataTable and this DataTable is passes to the Session variable that can be used to display the user selected items on any other ASP.Net web page throughout the session.

Download the free C# sample code for GridView Multiple Row Select

ajax gridview multi select.zip (3.56 kb)

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

7/24/2008 5:21:41 PM

OUR SPONSORS[+ advertise here]
top54u ezines.....
related videos.....
recent posts.....