Category >
ASP.NET
|| Published on :
Wednesday, January 24, 2018 || Views:
8990
||
GridView in ASP.Net using c# tutorial GridView GridView in ASP.Net
Introduction
Here Pawan Kumar will explain how to code gridview in asp.net c# tutorial
Description
In previous post I have explained
How to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator,
Set or Display Watermark Text for ASP.Net TextBox, Password and MultiLine TextArea using jQuery in ASP.Net with demo and Example codes,
Create Login Page In ASP.NET Web Application Using ASP.Net, C# And SQL Server,
Perserve/Retain state for dynamically created ASP.Net Dynamic Controls ViewState on PostBack,
Allow only AlphaNumeric ( Alphabets and Numbers) characters and space only in TextBox using Javascript and jQuery(Example & Demo),
Export HTML Table to PDF in ASP.Net with C# using iTextSharp DLL Library, and many more articles.
Now I will explain How to GridView in ASP.Net using c# tutorial
So follow the steps to learn GridView in ASP.Net using c# tutorial
Method 1: Using SQLDataSource ASP.NET Control
In this tutorial we are going to learn and code grid view in asp.net with example. When We have to show data in tabular form the we use ASP.net gridview.
The ASP.Net GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control. The ASP.Net GridView control can be bound to a data source control, in order to bind a data source control, set the DataSourceID property of the ASP.Net GridView control to the ID value of the data source control.
Now, let's start the coding for gridview in asp.net c# tutorial
Create a Database and table using the below SQL script.
CREATE DATABASE [aspDb]
Create a table using the below SQL script and filled with some dummy data.
USE [aspDb]
GO
/****** Object: Table [dbo].[Employee] Script Date: 24/01/18 3:53:45 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Employee](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[EmpName] [varchar](50) NOT NULL,
[Address] [varchar](50) NULL,
[Phone] [varchar](50) NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Employee] ON
GO
INSERT [dbo].[Employee] ([ID], [EmpName], [Address], [Phone]) VALUES (1, N'Raju', N'12, Karol Bagh', N'125252')
GO
INSERT [dbo].[Employee] ([ID], [EmpName], [Address], [Phone]) VALUES (2, N'Kamal', N'23, Aram Nagar', N'25363')
GO
INSERT [dbo].[Employee] ([ID], [EmpName], [Address], [Phone]) VALUES (3, N'Rita', N'45, Karol Bagh', N'1252252')
GO
SET IDENTITY_INSERT [dbo].[Employee] OFF
GO
Create a ASP.Net website using Visual Studio.
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="SQLDbConnection"
connectionString="Server=.; Database=aspDb; Integrated Security=true;
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Add ASPX webpage and add a "GridView" & "SqlDataSource" from toolbar. After adding HTML Markup will be like below:-
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:con %>"
SelectCommand="select EmpName, Address, Phone from Employee" />
</div>
</form>
</body>
</html>
We have bind the SQLDataSource with Gridview and in select command we have written SQL select statment to fetch employee record.
Method 2: Bind the GridView from Code Behind at runtime using C#
Include the below namespaces in code behind
using System.Data;
using System.Data.SqlClient;
ASPX HTML Markup
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_102_Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Code Behind
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _102_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string sql = null;
string connetionString = "Server=.; Database=aspDb; Integrated Security=true;";
sql = "select * from employee";
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
Conclusion:
So, In this tutorial we have learned, GridView in ASP.Net using c# tutorial
Download Source Codes