Saturday, 31 August 2013

What is Grid View.? Binding Expressions in GridView..?



GridView:

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


Syntax:

 
<asp:GridView ID="GV1" runat="server" AutoGenerateColumns="false">  
      <Columns>  
           <asp:BoundField.../>  
           <asp:TemplateField.../>  
           <asp:CommandField.../>  
      </Columns>  
</asp:GridView>  
 
BoundField , Template Field ... are Grid View 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.


Data Binding Expressions:

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

Syntax:
      <%# Eval (< fieldname>) %>  
      <%# Bind()%>  
 
Eval and Bind both are used to retrieve data but Eval is one-way method, which means retrieving from Data Object 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.

Ex:
 
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 Auto Generate columns to false . So, that we can specify our own columns information. Go to Source View of grid and write the markup using Grid view Fields and Data Binding 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 Data Binding Controls like Repeater , Data List also we use Templates.
A part from Eval & Bind they can also write Data Binding Expressions to perform some calculations as well as to run user defined 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();  
   }  
 }  


Conclusion:

In this article I'm trying to explain the GridView usages and Binding expression in grid view with small sample application.