Skip to main content

Nasted GridView using Entity DataModel


Nasted GridView using Entity DataModel


Page View:-


Under showdata button_click
protected void Button1_Click(object sender, EventArgs e)
{
   try
    {
    Utility obj = new Utility();
    GridView1.DataSource = obj.Getrest();
    GridView1.DataBind();
    for (int i = 0; i < GridView1.Rows.Count; i++)
      {
        GridView gv = (GridView)GridView1.Rows[i].FindControl("GridView2");
        Label bv = (Label)GridView1.Rows[i].FindControl("lbl");
        gridviewModel.menu obj1=new gridviewModel.menu ();
        gv.DataSource = obj.Getmenu(bv.Text);
        gv.DataBind(); 
      }
    }
   catch (Exception ex)
   {
   Response.Write(ex.Message);
   }
 }

Gridview Source Code:-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  Height="183px" Width="584px" CellPadding="4" ForeColor="#333333"
  GridLines="None">
  <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  <Columns>                       
  <asp:TemplateField HeaderText ="Rid">
  <ItemTemplate>
  <asp:Label runat="server" ID="lbl"  
  Text='<%#Eval("Rid") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Hotel Name">
  <ItemTemplate>
  <asp:Label runat="server" ID="lbl1" Text='<%#Eval("name") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Location">
  <ItemTemplate>
  <asp:Label runat="server" ID="lbl2" 
  Text='<%#Eval("location") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Menus">
  <ItemTemplate>
  <asp:GridView ID="GridView2" runat="server"  AutoGenerateColumns="false"
  style="margin-right: 2px"
  Width="369px" BackColor="White"
  BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
  <Columns>                       
  <asp:TemplateField HeaderText="Mid">
  <ItemTemplate>
  <asp:Label ID="Label1" runat="server" 
  Text='<%#Eval("mid") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Item Name">
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server"           
  Text='<%#Eval("itemname")%>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText ="Price">
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server"  
  Text='<%#Eval("price") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>                                           
  </Columns>
  <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  <HeaderStyle BackColor="Indigo" Font-Bold="True" ForeColor="#CCCCFF" />
  <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left"/>
  <RowStyle BackColor="White" ForeColor="#003399" />
  <SelectedRowStyle BackColor="#009999" Font-Bold="True"
  ForeColor="#CCFF99"/>
  <SortedAscendingCellStyle BackColor="#EDF6F6" />
  <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
  <SortedDescendingCellStyle BackColor="#D6DFDF" />
  <SortedDescendingHeaderStyle BackColor="#002876" />
  </asp:GridView>
  </ItemTemplate>
  </asp:TemplateField>
  </Columns>
  <EditRowStyle BackColor="#999999" />
  <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
  <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"/>
  <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"/>
  <SortedAscendingCellStyle BackColor="#E9E7E2" />
  <SortedAscendingHeaderStyle BackColor="#506C8C" />
  <SortedDescendingCellStyle BackColor="#FFFDF8" />
  <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
  </asp:GridView>

Add ADO.NET Entity Data Model under App_code and select database tables







  add new class Utility.cs and write code under utility class
Under Utility Class:-
 public List<gridviewModel.rest> Getrest()
   {
      gridviewModel.gridviewEntities obj = new gridviewModel.gridviewEntities();
      var x = from n in obj.rests select n;
      return x.ToList<gridviewModel.rest>();
   }
public List<gridviewModel.menu> Getmenu(object ss)
   {
      int s = int.Parse(ss.ToString());
      gridviewModel.gridviewEntities obj = new gridviewModel.gridviewEntities();
      var c=from n in obj.menus where n.Rid==s select n;
      return c.ToList<gridviewModel.menu>();
   }

Comments

Popular posts from this blog

GRIDVIEW GROUPING

When displaying data, we sometimes would like to group data for better user experience or when displaying long list of hierarchal data, we would want to display them in a tree view kind of structure. There is more than way of doing this, but I am going to explain achieving this functionality using  AJAX Collapsible Panel Extender Control . Overview: I am going to use  Adventure Works  as datasource. Every product in  Production.Product  table belongs to a product sub category. We fetch handful of products and the sub categories they belong to from the database. Our objective is to list all the available sub categories and allow user to  expand/collapse  to look/hide the list of products belonging to each subcategory. Database Connection Added following entry under  connectionStrings  element in  web.config . < add   name = "Sql"   connectionString="Data Source=(local); Initial  Catalog = AdventureWorks ...

ADO.NET Concepts With example

COMMAND OBJECT  Command object is the biggest object in ADO.NET  It is the only object which can perform actions with database  It  can be created directly using Command class or can be created using Connection.Create command (factory classes also contain creation of Command). Ex:  SqlConnection sqlCon = new SqlConnection("......");    SqlCommand sqlCmd = sqlCon.CreateCommand();  Commands that we run depend upon the kind of query that we want to execute with database. All databases support two types of queries. i. Action Queries ii. Non-action Queries Action queries are those which change the state of database and which don‟t return any query results(though they return the number of records affected). Ex: Insert, Delete and Update statements Non-action queries are those which don‟t affect the database but return the results to the user. Ex: Select statement Method of execution of queries: Command object provides the following methods to execute queries: 1. Ex...

GRIDVIEW ZOOM IMAGE

When you have images in your  GridView , you would most likely show them as thumbnails so as to not distort the whole layout. However user would want to look at the full image by clicking on the image or just hovering his mouse over it. In today’s applications, this is a basic requirement and there are just so many third party controls or plugins which would support this functionality. I am going do this conventional way using  javascript  way in this article.  On top of it, I am also going to explain how to get images from database using  HttpHandlers . Example: I am using  Adventure Works  as datasource. We fetch handful of products and bind them to the grid. When page is initially loaded, we retrieve products from Production . Product  table and bind them to the grid. We display some product attributes such as Product ID, Product Number, Product Name, List Price and product’s thumbnail. When user hover his mouse on the page, we fetch the f...