How to Get a Multiple selected Values from a Gridview using Asp.Net

Syedshakeer
Posted by in ASP.NET category on for Intermediate level | Views : 9846 red flag

In this Article you can Know how to get a Multiple selected values using CheckBox

First Drag and Drop one Gridvew and Button on asp Page.Later Retrive the Data From a Table on Gridview,by using Bound field and Add on CheckBox field in .aspx source code page as Below.

<asp:GridView id="Gridview1" runat ="server" AutoGenerateColumns ="False" ShowFooter ="True"  DataSourceID ="SqlDataSource1" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" style="z-index: 101; left: 19px; position: absolute; top: 142px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" Width="240px" >

 

<Columns >

<asp:TemplateField >

<ItemTemplate >

<asp:CheckBox ID="chk" runat ="server" />

</ItemTemplate>

</asp:TemplateField>

 

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

<asp:BoundField DataField="empname" HeaderText ="Name" />

<asp:BoundField DataField ="salary" HeaderText ="Salary" />

 

</Columns>

    <FooterStyle BackColor="Tan" />

    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

    <HeaderStyle BackColor="Tan" BorderColor="Fuchsia" BorderStyle="Solid" Font-Bold="True" />

    <AlternatingRowStyle BackColor="PaleGoldenrod" />

</asp:GridView>

 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [ID], [empname], [salary] FROM [emp]">

        </asp:SqlDataSource>

The output Image of Above code is Below: 

Now i am writing code in GetValues Button to retrive values when the user selects one or more CheckBoxes.

protected void GetValues_Click(object sender, EventArgs e)

    {

        for (int i = 0; i < Gridview1.Rows.Count; i++)

        {

            CheckBox chkb = (CheckBox)Gridview1.Rows[i].Cells[0].FindControl("chk");

            if (chkb.Checked)

            {

                string name = Gridview1.Rows[i].Cells[2].Text;

                Response.Write("<br>" + name);

            }

        }

    }

 

Click on GetValues Button After selecting one or more CheckBoxes, then you will get Values what you had checked in gridview.

The OutPut Of Image is Below:

 


 

Page copy protected against web site content infringement by Copyscape

About the Author

Syedshakeer
Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
http://www.dotnetfunda.com
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)