Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 13 April 2015

Introduction

In this article, I will explain how to bind data to gridview in asp.net using dataset with example using c#, vb.net .


Implementation:  Let's create a demo website to demonstrate the concept.
Create a database “Test”, Then create one table i.e. Emp_Personal
  Column Name
Data Type
EmpPer_Id
Int(Primary Key. So set is identity=true)
EmpName
varchar(100)
Age
Int
Address
varchar(500)

Source Code: Sample Application
Design Page: design your page as given below
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Bind data to gridview in asp.net using c#, vb.net</title>

</head>
<body>
<form id="form1" runat="server">
<fieldset style="width:300px">
    <legend><strong>Paging in Gridview</strong></legend>
<div class="GridviewDiv">
<asp:GridView runat="server" ID="gvDetails" AllowPaging="true" PageSize="5" AutoGenerateColumns="false" Width="350px" OnPageIndexChanging="gvDetails_PageIndexChanging">
<HeaderStyle BackColor="#620303" ForeColor="#ffffff" />
<Columns>
<asp:BoundField DataField="EmpPer_Id" HeaderText="Employee Id" />
<asp:BoundField DataField="EmpName" HeaderText="Employee Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
</Columns>
</asp:GridView>
</div></fieldset>
</form>
</body>
</html>


ASP.NET Code behind File(c#):
Write following code in code behind file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;

public partial class paging : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    protected void BindGridview()
    {
        DataSet ds = new DataSet();
        using (SqlConnection con = new SqlConnection("Data Source=localhost;Integrated Security=true;Initial Catalog=Test"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Emp_Personal", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Close();
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
        }
    }
    protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDetails.PageIndex = e.NewPageIndex;
        BindGridview();
    }

}

ASP.NET Code behind File (VB.NET):

Write following code in code behind file:

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControls

Partial Public Class paging
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            BindGridview()
        End If
    End Sub
    Protected Sub BindGridview()
        Dim ds As DataSet = New DataSet()
        Imports (SqlConnection con = New SqlConnection("Data Source=localhostIntegrated Security=TrueInitial Catalog=Test"))
        {
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand("select * from Emp_Personal", con)
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        da.Fill(ds)
        con.Close()
        gvDetails.DataSource = ds
        gvDetails.DataBind()
        }
    End Sub
    Protected Sub gvDetails_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        gvDetails.PageIndex = e.NewPageIndex
        BindGridview()
    End Sub


End Class
Categories: , ,

0 comments:

Post a Comment