How to Get Selected Row Cell Value From The GridView in ASP.Net Web Forms using VB.NET

Category > VBVB.NET || Published on : Thursday, October 22, 2020 || Views: 70 || Get Selected Row Cell Value From The GridView


Here Pawan Kumar will explain how to Get Selected Row Cell Value From The GridView in ASP.Net Web Forms using VB.NET

VB.ASPX Source Codes:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB.aspx.vb" Inherits="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>  VB.NET Source Code - How to Get Selected Row Cell Value From The GridView in ASP.Net Web Forms  using VB.NET - www.sourcecodehub.com</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
        <form id="form1" runat="server">
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-heading">
                        <div class="panel-heading text-center">
                            <h1>Hotel Management System</h1>
                        </div>
                    </div>
                </div>
                <div class="col-lg-8">
                    <div class="panel panel-default">
                        <div class="panel-heading">Guest List</div>
                        <div class="panel-body">
                            <asp:GridView ID="grdGuest" CssClass="table table-bordered" runat="server"
                                AutoGenerateColumns="false" OnSelectedIndexChanged="grdgrdGuest_SelectedIndexChanged">
                                <Columns>
                                    <asp:BoundField DataField="GuestId" HeaderText="EmployeeId" ItemStyle-Width="100" />
                                    <asp:BoundField DataField="GuestName" HeaderText="EmployeeName" ItemStyle-Width="150" />
                                    <asp:BoundField DataField="Address" HeaderText="Department" ItemStyle-Width="150" />
                                    <asp:BoundField DataField="Mobile" HeaderText="Designation" ItemStyle-Width="150" />
                                    <asp:ButtonField Text="Select" ControlStyle-CssClass="btn btn-primary" CommandName="Select" ItemStyle-Width="50" />
                                </Columns>
                            </asp:GridView>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4">
                    <div class="panel panel-default">
                        <div class="panel-heading">Guest Details</div>
                        <div class="panel-body">
                            <asp:Label ID="lblGuestDetails" runat="server"></asp:Label>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</body>
</html>

VB.aspx.vb - Code Behind Source Codes:-

Imports System.Data
Imports System.Drawing
Partial Class VB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            If Not Me.IsPostBack Then
                grdGuest.DataSource = GetGuestData()
                grdGuest.DataBind()
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub

    Private Function GetGuestData() As DataTable
        Try
            Dim dt As DataTable = New DataTable
            dt.Columns.AddRange(New DataColumn() {New DataColumn("GuestId"), New DataColumn("GuestName"), New DataColumn("Address"), New DataColumn("Mobile")})
            dt.Rows.Add(5001, "Raman Singh", "23, Karol Bagh", "56998989")
            dt.Rows.Add(5002, "Raj Kumar", "5699, Mandir Marg", "66998989")
            dt.Rows.Add(5003, "Rahul Kumar", "223, Punjabi Bagh", "76998989")
            dt.Rows.Add(5004, "Pawan Kumar", "458, Greater Noida", "96998989")
            dt.Rows.Add(5005, "Suman Kumar", "556,. Pahar Ganj", "56998989")
            Return dt
        Catch ex As Exception
            Throw
        End Try

    End Function

    Protected Sub grdgrdGuest_SelectedIndexChanged(sender As Object, e As EventArgs)
        Try
            Dim EmployeeId As String = grdGuest.SelectedRow.Cells(0).Text
            Dim EmployeeName As String = grdGuest.SelectedRow.Cells(1).Text
            Dim Department As String = grdGuest.SelectedRow.Cells(2).Text
            Dim Designation As String = grdGuest.SelectedRow.Cells(3).Text
            Dim CompanyName As String = grdGuest.SelectedRow.Cells(4).Text
            lblGuestDetails.Text = "<b>GuestId:</b> " & EmployeeId & " </br><b>GuestName:</b> " & EmployeeName & " </br><b>Address:</b> " & Department & " </br><b>Mobile:</b> " & Designation
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Class