Monday, 1 July 2013

USING EVAL METHOD, BIND A GRIDVIEW TO DATABASE TABLE.


Introduction:

In this article I will explain how to Bind the sql server database table value to grid view using Eval method.
Description:

I have to design one grid view I need to write code to bind a data into grid view using eval method.

Step1.

To design a grid view used below code.

<asp:GridView ID="test11" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" HeaderStyle-BackColor="#61A6F8"  ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" >

Step2.

To Manually Create columns to the grid view using Column keyword in asp.net.Then add the item Template for to fetch the Database table field, Refer below code.


<Columns>
<asp:TemplateField HeaderText="Meeting Date">

<ItemTemplate >
<asp:Label ID="Coloumn1" runat ="server" text=' <%# Eval("Mdate")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>

 Step 3.

To establish a connection into the Database using Connection string at webconfig file.

<connectionStrings>
<add name="connectiontosql"     connectionString="Data Source=PROV-PC-02\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"
providerName="System.Data.SqlClient" />

</connectionStrings>


Step 4.

Using data adapter to fetch a database table data and filled into dataset.

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(query, EstablishtoDB);
da.Fill(ds);

Step 5.

My full implementation source.

Aspx Page design.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Anand_Practice.WebForm1" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="test11" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" HeaderStyle-BackColor="#61A6F8"  ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" >

<Columns>
<asp:TemplateField HeaderText="Meeting Date">

<ItemTemplate >
<asp:Label ID="Coloumn1" runat ="server" text=' <%# Eval("Mdate")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>

<Columns>
<asp:TemplateField HeaderText="Meeting from">

<ItemTemplate >
<asp:Label ID="Coloumn1" runat ="server" text=' <%# Eval("Mfrom")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>

<Columns>
<asp:TemplateField HeaderText="UserName">

<ItemTemplate >
<asp:Label ID="Coloumn1" runat ="server" text=' <%# Eval("Uname")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>

</asp:GridView>
</div>
</form>
</body>
</html>

Now add the following namespaces in code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;
using System.Data;



protected void Page_Load(object sender, EventArgs e)
{

fillgrid();

}



public void fillgrid()
{

string connection = ConfigurationManager.ConnectionStrings["connectiontosql"].ToString();
string query = " select *  from Data_Load_New ";
using (SqlConnection EstablishtoDB = new SqlConnection(connection))
{
EstablishtoDB.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(query, EstablishtoDB);
da.Fill(ds);
test11.DataSource = ds;
test11.DataBind();
}

}
  
Final result will look like this. 



Thanks...

No comments:

Post a Comment