Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Monday, 11 April 2016

Display Data in GridView From Database Using Asp.Net

 asp.net, gridview     No comments   

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();
  }



  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

comma seperated string in sql Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS row_number() and dense_rank() salary table structure. Scope_Identity() Use of indexes while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write