Introduction
In this article we will learn what is asp gridview control. Why we need Gridview control . How can we display the data with in gridview.
GridView- Control
GridView control is a successor to the ASP.NET 1.X DataGrid control and and extends it in a number of ways.. It provides more flexibility in displaying and working with data from your database in comparison with any other controls. The GridView control enables you to connect to a datasource and display data is tabular format, however you have bunch of options to customize the look and feel.
With this GridView control, you could display an entire collection of data, easily add sorting and paging, and perform inline editing. In addition to just displaying data, the GridView can be used to edit and delete the displayed data as well.
Display Data in GridView
We can use different ways to ind and display data in GridView, but the most common and easy way to do this is by using DataSource and DataBind Property from code-behind. So first we have to Create a database having multiple records according to our need and then connect this database from our Asp.Net WebApplication.
On Sql Server write following queries:-
Create Database Employee
Use Employee
Create table EmpDetail( Id int Identity(1,1), Name varchar(50),Department
varchar(50),DOB date,Salary Decimal(12,0),
Address varchar(max))
|
And after creating the database and table Insert record as per your need.Now here I am using Stored-Procedure for to retrieve data from Database.
Procedure:
Create proc [dbo].[spbindemp]
as begin
select Id, Name,Department,DOB,Salary, Address from EmpDetail
end
|
Now on your Asp.Net WebApplication Create a simple WebForm and Use GidView Contol by simply drag and drop. Here I am providing the code of my GridView Control
Aspx Page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmployeeDetails" runat="server" Width="100"
HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false" Height="111px">
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lbldep" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "Department") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB">
<ItemTemplate>
<asp:Label ID="lbldob" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem,"DOB","{0:MM/dd/yy}")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sallary">
<ItemTemplate>
<asp:Label ID="lblsallary" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "Salary") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lbladdress" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "Address") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
Go to Code Behind Window by double clicking on Page and write C# code for bind this GridView with Database
CS Page
protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
protected void BindGridView()
{
SqlConnection con = new SqlConnection("Data Source=.;
Initial Catalog=Employee;Integrated Security=True");
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
SqlCommand cmd = new SqlCommand("spbindemp", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
FromTable = ds.Tables[0];
if (FromTable.Rows.Count > 0)
{
gvEmployeeDetails.DataSource = FromTable;
gvEmployeeDetails.DataBind();
}
else
{
FromTable.Rows.Add(FromTable.NewRow());
gvEmployeeDetails.DataSource = FromTable;
gvEmployeeDetails.DataBind();
int TotalColumns = gvEmployeeDetails.Rows[0].Cells.Count;
gvEmployeeDetails.Rows[0].Cells.Clear();
gvEmployeeDetails.Rows[0].Cells.Add(new TableCell());
gvEmployeeDetails.Rows[0].Cells[0].ColumnSpan = TotalColumns;
gvEmployeeDetails.Rows[0].Cells[0].Text = "No records Found";
}
ds.Dispose();
con.Close();
} |
0 comments:
Post a Comment