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 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...

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {     ...

Scrollable GridView with Fixed Headers using jQuery Plugin

Using the same example I have created a jQuery Plugin for Scrollable GridView with Fixed header so that you can directly make a GridView scrollable.   HTML Markup < form   id ="form1"   runat ="server"> < asp : GridView   ID ="GridView1"   runat ="server"   AutoGenerateColumns   =   "false"> < Columns > < asp : BoundField   DataField   =   "ContactName"   HeaderText   =   "Contact Name"   /> < asp : BoundField   DataField   =   "City"   HeaderText   =   "City"   /> < asp : BoundField   DataField   =   "Country"   HeaderText   =   "Country"   /> Columns > asp : GridView > form >   Applying the Scrollable Grid jQuery Plugin < script   src ="Scripts/jquery-1.4.1.min.js"   type ="text/javascript"> script > < script   src ="Scripts/Scro...