Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Tuesday, 31 March 2015

Introduction:
In this article  I will explain how to add checkboxes in asp.net gridview and get gridview selected rows data on button click in asp.net  and then we can work with the data for further processing.




Following Steps we will cover in this article:
1.    Create Dynamic table in code behind in asp.net.
2.    Use that table as data source for gridview.
3.    Add check boxes in gridview.
4.    Get selected row value from gridview on button click.
5.    Show selected value in Label Control in asp.net.

Full Source Code:

Design section:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get Checkbox Selected Rows in Asp.net Gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView AutoGenerateColumns="false" ID="gvDetails" CellPadding="5" runat="server">
<HeaderStyle BackColor="#4D0101" Font-Bold="true" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkDetails" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpID">
<ItemTemplate>
<asp:Label id="lblEmpid" runat="server" Text='<%#Eval("EmpId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label id="lblEmpname" runat="server" Text='<%#Eval("Empname") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label id="lblDesignation" runat="server" Text='<%#Eval("Designation") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<br />
<asp:Button ID="btngetdetails" Text="Get Selected value" runat="server"
onclick="btngetdetails_Click" BackColor="#4D0101" ForeColor="White" />
<br /><br />
<b>Selected Values are:</b><asp:Label ID="lblselected" runat="server" />
</form>
</body>
</html>

ASP.Net Code behind File(C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;


public partial class gridvalue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridviewData();
        }
    }
    protected void BindGridviewData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("EmpId", typeof(Int32));
        dt.Columns.Add("EmpName", typeof(string));
        dt.Columns.Add("Designation", typeof(string));
        dt.Rows.Add(1, "Anil Minhas", "HR");
        dt.Rows.Add(2, "Ankush rana", "Designer");
        dt.Rows.Add(3, "Aman Kamboj", "Designer");
        dt.Rows.Add(4, "Vijay Saklani", "Developer");
        dt.Rows.Add(6, "Ajay", "Developer");
        dt.Rows.Add(7, "Mayank", "Designer");
        dt.Rows.Add(8, "Mannu", "QA");
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void btngetdetails_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        string str = "";
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("EmpId"), new DataColumn("Status") });
        foreach (GridViewRow row in gvDetails.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox bf = (row.Cells[3].FindControl("chkDetails") as CheckBox);
                string id = (row.Cells[0].FindControl("lblEmpid") as Label).Text;

                if (bf.Checked)
                {
                    str = str + id + ",";
                }
            }
        }
        str = str.Remove(str.Length - 1, 1);
        lblselected.Text = str;
    }

}

ASP.Net Code behind File(Vb.Net)

Imports System.Data
Imports System.Web.UI.WebControls


Partial Public Class gridvalue
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            BindGridviewData()
        End If
    End Sub
    Protected Sub BindGridviewData()
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("EmpId", Type.GetType(Int32))
        dt.Columns.Add("EmpName", Type.GetType(Of String))
        dt.Columns.Add("Designation", Type.GetType(Of String))
        dt.Rows.Add(1, "Anil Minhas", "HR")
        dt.Rows.Add(2, "Ankush rana", "Designer")
        dt.Rows.Add(3, "Aman Kamboj", "Designer")
        dt.Rows.Add(4, "Vijay Saklani", "Developer")
        dt.Rows.Add(6, "Ajay", "Developer")
        dt.Rows.Add(7, "Mayank", "Designer")
        dt.Rows.Add(8, "Mannu", "QA")
        gvDetails.DataSource = dt
        gvDetails.DataBind()
    End Sub
    Protected Sub btngetdetails_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim dt As DataTable = New DataTable()
        Dim str As String = ""
        Dim DataColumn(2) As dt.Columns.AddRange(New
        {
             New DataColumn("EmpId"), New DataColumn("Status")
        }
)
        Dim row As GridViewRow
        For Each row In gvDetails.Rows
            If row.RowType = DataControlRowType.DataRow Then
                Dim bf As CheckBox = (row.Cells(3).FindControlCType(as CheckBox, "chkDetails"))
                Dim id As String = (row.Cells(0).FindControlCType(as Label, "lblEmpid")).Text

                If bf.Checked Then
                    str = str + id + ","
                End If
            End If
        Next
        str = str.Remove(str.Length - 1, 1)
        lblselected.Text = str
    End Sub


End Class
Categories: , ,

0 comments:

Post a Comment