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

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        {              border : solid1px#c0c0c0;              background : #f0f0f0;              padding : 0px10px10px10px;              position : absolute;              top : -1000px;       } </ style > Step 3:   Create an aspx page and add a Gridview with another gridview in the last TemplateField. The last templatefield will also contain a lable which will

Hierarchical GridView in ASP.NET with AJAX, JQuery implementation - Part 1

Previously I had blogged some post related to showing two grids in hierarchical way. For example - when I need to show an invoice reports on the screen, the user requesting to show the list of invoices at initial time and they can drill down the details of item details of each invoice by clicking on respective row. The implementations can be in multiple ways, but I had blogged the implementation with AJAX and without AJAX using Java script on ASP.NET page. Below are the URLs of the same – Hierarchical GridView in ASP.NET Hierarchical GridView in ASP.NET with AJAX, Javascript implementation In this post I am taking the same requirements done before and enhance with some additional functionality as per readers expectations. The requirement on this implementation will be – The page should show a Grid View with list of Orders with Expand icon on the first column. On click of expand icon on each row, the Order Details of the Order must be fetched from the database

Scrollable Gridview With fixheader using JQuery in Asp.net

Scrollable Gridview With fixheader using JQuery in Asp.net Introduction: In this article I will explain how to implement scrollable gridview with fixed header in asp.net using JQuery.  Description:  In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement scrollable gridview with fixed header in asp.net. I have one gridview that contains lot of records and I used  paging for gridview  but the requirement is to display all the records without paging. I removed paging at that time gridview occupied lot of space because it contains more records to solve this problem we implemented scrollbar.  After scrollbar implementation if we scroll the gridview we are unable to see Gridview header.   To implement Scrollable gridview with fixed header I tried to implement concept with css and JavaScript but there is no luck because maintaining fixed header working in IE but not in Mozilla and vice versa to solve this browser compatibility proble