What is GridView.? Binding Expressions in Gridview.?


What is GridView.? Binding Expressions in Gridview.? In this artical I am trying to explain about what is GridView..? and What are the different types of Binding Expressions in Gridview..? and How to create Custom DataBinding Expressions in Gridview.?

GRIDVIEW:



Presentation of data purpose we can use Gridview control. For
effective presentations of data we have to handle Gridview columns display
manually. For this we can set AutoGenerate column property to false is
provide all column information manually.


Syantax:


<asp:GridView ID="GV1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField.../>
<asp:TemplateField.../>
<asp:CommandField.../>
</Columns>
</asp:GridView>

BoundField , Template Field ... are GridView Predefined columns which are
used for presenting the Data. Different fields provide different
functionality and according to use we will write MarkUp with particular
fields.


DataBinding Expressions:

When we write GridView Fields as part of columns collections, then
retrieving data from Data Object is performed using DataBinding
Expressions. EveryField when requires the Data either it's property should
get it from DataObject (or) By using DataBinding Expression we can get the
Data for ex " Bound Field " has a property called as DataField. To get
data from Data Object Template Field doesn't have any property for
presenting (or) retrirving the data . So, it uses Data Binding Expression
for retrieving data. ASP.net provide a separate block for presenting (
or ) writing DataBinding Expressions.


Syntax:

<%# Eval (< fieldname>) %>
<%# Bind()%>

Eval and Bind both are used to retrieve data but Eval is one-way method,
which means retrieving from DataObject to control, where as Bind is two-
way method, where we retrieve as well as update the data. Bind is very
less use ,because of it's conditional usage..

BoundField is used for simple presentation where as Template Field
for any type of output.



EXAMPLE:


1) In a form place a Button & GridView Controls

2) In Button Click event write the code to prepare Data Object &
assign same to GridView.

3) Set AutoGenerate columns to false . So, that we can specify our
own columns information. Go to SourceView of grid and write the markup
using Gridview Fields and DataBinding Expressions.


NOTE : Template Field is a collections of templates. A template is
collection of different controls. We can produce ( or ) design any output
using templates. In other DataBinding Controls like Repeater , DataList
also we use Templates.

A part from Eval & Bind they can also write DataBinding
Expressions to perform some calculatons as well as to run user defind
members of class.


Source Code :



<asp:GridView ID="GV" runat="server" AutoGenerateColumns="False"
ShowFooter="true" >
<Columns>
<asp:TemplateField HeaderText="Metal" HeaderStyle-ForeColor="White" >
<ItemTemplate>
<asp:Label ID="lblMaterial" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"Material") %>'/>
<asp:Label ID="lblMaterial_Id" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"Material_Id") %>'
Visible="false"/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Type" HeaderStyle-
ForeColor="White" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblItem" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"Item_Type") %>'/>
<asp:Label ID="lblItem_Id" runat="server" Text='<%
#DataBinder.Eval(Container.DataItem,"Item_Id") %>'
Visible="false"/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Unit" HeaderStyle-
ForeColor="White" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblUnit" runat="server" Text='<%
#DataBinder.Eval(Container.DataItem,"Unit") %>' />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Qty" HeaderStyle-
ForeColor="White" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblqty" runat="server" Text='<%
#DataBinder.Eval(Container.DataItem,"Quantity") %>'/>
</ItemTemplate>
</asp:TemplateField>

<%--Custom DataBinding Expressions--%>
<asp:TemplateField HeaderText="Level" HeaderStyle-
ForeColor="White">
<ItemTemplate>
<%#GetLevel(Eval("Material"))%>
</ItemTemplate>
<FooterTemplate>
<%#GetTotal()%>
</FooterTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>



Code Behind:




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Syste.Data.SqlClient;
using System.Web.Services;

public partial class GV : System.Web.UI.Page
{
string Code;
double UserId;
string Web_Login;
int a;

protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);

SPCurrentSite objCurrentSite = new SPCurrentSite();
this.MasterPageFile = objCurrentSite.MasterURL;
}
protected void Page_Load(object sender, EventArgs e)
{
SPCurrentSite objCurrentSite = new SPCurrentSite();
Design objDesign = new Design();

Code = objCurrentSite.WebSiteName;
SPCurrentUser objCurrentUser = new SPCurrentUser();

Web_Login = objCurrentUser.LoginName;
UserId = objDesign.WebLogin(Web_Login);

if (!IsPostBack)
{
Bind_GV();
}


}

//custom DataBinding expressions
public string GetTotal()
{
return "Total:" + a.ToString();
}
//custom DataBinding expressions
public string GetLevel(object Material)
{
a++;
if (Material.ToString().ToUpper().Contains("RCC"))
return "high";
else
return "low";
}

//Bind data to GridView
protected void Bind_GV()
{
Design obj = new Design();
DataSet ds = //Bind Data;

GV.DataSource = ds;
GV.DataBind();

}
}


Attachments

Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: