Friday, December 30

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>();
   }


Thanks 
       shibashish mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty