Introduction
Requirement
Implementation
What is GridView in asp.net web forms?
What is bootstrap?
How to integrate bootstrap with asp.net webform application?
Integrate bootstrap CDN with your ASP.NET webforms
<head runat="server"> <title></title> <meta charset="utf-8" /> <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 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head>
Fetch selected row cell value from GridView in asp.net webform
Employees.aspx
<!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></title> <meta charset="utf-8" /> <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 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script 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>Employee Management System</h1> </div> </div> </div> <div class="col-lg-8"> <div class="panel panel-default"> <div class="panel-heading">Employee List</div> <div class="panel-body"> <asp:GridView ID="grdEmp" CssClass="table table-bordered" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="grdEmp_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" ItemStyle-Width="100" /> <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" ItemStyle-Width="150" /> <asp:BoundField DataField="Department" HeaderText="Department" ItemStyle-Width="150" /> <asp:BoundField DataField="Designation" HeaderText="Designation" ItemStyle-Width="150" /> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 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">Employee Details</div> <div class="panel-body"> <asp:Label ID="lblEmployeeDetails" runat="server"></asp:Label> </div> </div> </div> </div> </form> </div> </body> </html>
Explanation
C#
private DataTable GetEmployeeData() { try { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[5] { new DataColumn("EmployeeId"), new DataColumn("EmployeeName"), new DataColumn("Department"), new DataColumn("Designation"), new DataColumn("CompanyName") }); dt.Rows.Add(1001, "Nikunj Satasiya", "Computer/IT", "Software Engineer", "Casepoint LLC."); dt.Rows.Add(1002, "Hiren Dobariya", "Computer/IT", "Software Engineer", "Version System Pvt.Ltd."); dt.Rows.Add(1003, "Vivek Ghadiya", "Sales Department", "Sales Executive", "Balaji Wafers Pvt.Ltd."); dt.Rows.Add(1004, "Pritesh Dudhat", "Networking", "Network Engineer", "Narola Infotech"); dt.Rows.Add(1005, "Priya Patel", "Computer/IT", "Software Engineer", "Thomson Reuters India Pvt.Ltd."); return dt; } catch (Exception) { throw; } }
VB.NET
Private Function GetEmployeeData() As DataTable Try Dim dt As DataTable = New DataTable dt.Columns.AddRange(New DataColumn() {New DataColumn("EmployeeId"), New DataColumn("EmployeeName"), New DataColumn("Department"), New DataColumn("Designation"), New DataColumn("CompanyName")}) dt.Rows.Add(1001, "Nikunj Satasiya", "Computer/IT", "Software Engineer", "Casepoint LLC.") dt.Rows.Add(1002, "Hiren Dobariya", "Computer/IT", "Software Engineer", "Version System Pvt.Ltd.") dt.Rows.Add(1003, "Vivek Ghadiya", "Sales Department", "Sales Executive", "Balaji Wafers Pvt.Ltd.") dt.Rows.Add(1004, "Pritesh Dudhat", "Networking", "Network Engineer", "Narola Infotech") dt.Rows.Add(1005, "Priya Patel", "Computer/IT", "Software Engineer", "Thomson Reuters India Pvt.Ltd.") Return dt Catch ex As Exception Throw End Try End Function
C#
protected void Page_Load(object sender, EventArgs e) { try { if (!this.IsPostBack) { grdEmp.DataSource = GetEmployeeData(); grdEmp.DataBind(); } } catch (Exception) { throw; } }
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Try If Not Me.IsPostBack Then grdEmp.DataSource = GetEmployeeData() grdEmp.DataBind() End If Catch ex As Exception Throw End Try End Sub
C#
protected void grdEmp_SelectedIndexChanged(object sender, EventArgs e) { try { string EmployeeId = grdEmp.SelectedRow.Cells[0].Text; string EmployeeName = grdEmp.SelectedRow.Cells[1].Text; string Department = grdEmp.SelectedRow.Cells[2].Text; string Designation = grdEmp.SelectedRow.Cells[3].Text; string CompanyName = grdEmp.SelectedRow.Cells[4].Text; lblEmployeeDetails.Text = "<b>EmployeeId:</b> " + EmployeeId + " </br><b>EmployeeName:</b> " + EmployeeName + " </br><b>Department:</b> " + Department + " </br><b>Designation:</b> " + Designation + " </br><b>CompanyName:</b> " + CompanyName; } catch (Exception) { throw; } }
VB.NET
Protected Sub grdEmp_SelectedIndexChanged(sender As Object, e As EventArgs) Try Dim EmployeeId As String = grdEmp.SelectedRow.Cells(0).Text Dim EmployeeName As String = grdEmp.SelectedRow.Cells(1).Text Dim Department As String = grdEmp.SelectedRow.Cells(2).Text Dim Designation As String = grdEmp.SelectedRow.Cells(3).Text Dim CompanyName As String = grdEmp.SelectedRow.Cells(4).Text lblEmployeeDetails.Text = "<b>EmployeeId:</b> " & EmployeeId & " </br><b>EmployeeName:</b> " & EmployeeName & " </br><b>Department:</b> " & Department & " </br><b>Designation:</b> " & Designation & " </br><b>CompanyName:</b> " & CompanyName Catch ex As Exception Throw End Try End Sub
Explanation
Output
Recommended Articles
1) Export All The Excel Sheets to DataSet in C# and VB.NET
2) Export JSON to CSV using JQuery/Javascript and Bootstrap in ASP.NET
3) Error 26: Error Locating Server/Instance Specified in SQL Server 2008
4) PIVOT and UNPIVOT in SQL Server with Example
5) Get Distinct Records From Datatable using LINQ C#