Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET

Many to Many relation with Entity Framework in ASP.NET GridView in a simplified way

Rate me:
Please Sign up or sign in to vote.
4.73/5 (11 votes)
14 Jul 2012CPOL3 min read 80.5K   1.9K   23  
Many To Many Relationship in ASP.NET WebForms and CRUD operations using Entity Framework.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="ManyToMany._Default" MetaKeywords="Title"
    MetaDescription="Entity Framework Example" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label runat="server" ID="lblMessage" ForeColor="Red"></asp:Label><br />
    <asp:Panel runat="server" ID="Pnl" BorderStyle="None" GroupingText="Add Product">
        <table class="style1">
            <tr>
                <td align="right">
                    Product Name
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProd" EnableViewState="false" />
                    <asp:RequiredFieldValidator runat="server" ID="ReqProductName" ControlToValidate="txtProd"
                        ErrorMessage="Product Name required." ValidationGroup="AddProuct" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td align="left">
                    <asp:Button ID="btnProd" Text="Add Product" runat="server" OnClick="btnProd_Click"
                        ValidationGroup="AddProuct" />
                </td>
            </tr>
        </table>
    </asp:Panel>
    <asp:Panel runat="server" ID="Panel1" BorderStyle="None" GroupingText="Add Customer">
        <table class="style1">
            <tr>
                <td align="right">
                    Customer Name
                </td>
                <td>
                    <asp:TextBox ID="txtCus" runat="server" EnableViewState="false" />
                    <asp:RequiredFieldValidator runat="server" ID="ReqCus" ControlToValidate="txtCus"
                        ErrorMessage="Customer Name required." ValidationGroup="AddCustomer" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Select Product
                </td>
                <td>
                    <asp:DropDownList runat="server" ID="drpProd" AppendDataBoundItems="true">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator runat="server" ID="ReqProd" ControlToValidate="drpProd"
                        ErrorMessage="Please select Product Name." ValidationGroup="AddCustomer" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td align="left">
                    <asp:Button ID="btnCust" Text="Add Customer" runat="server" OnClick="btnCust_Click"
                        ValidationGroup="AddCustomer" />
                </td>
            </tr>
        </table>
    </asp:Panel>
    <asp:Panel runat="server" ID="Panel2" BorderStyle="None" GroupingText="Customer & Product Details">
        <table class="style1">
            <tr>
                <td align="left">
                    <asp:GridView runat="server" ID="grdDetails" OnRowDeleting="grdDetails_RowDeleting"
                        OnRowEditing="grdDetails_RowEditing" 
                        OnRowUpdating="grdDetails_RowUpdating" OnRowCancelingEdit="grdDetails_RowCancelingEdit"
                        AutoGenerateColumns="false" onrowdatabound="grdDetails_RowDataBound"   >
                        <Columns>
                                                       
                              <asp:TemplateField>
                                <HeaderTemplate>
                                    Customer Id
                                </HeaderTemplate>
                                <ItemTemplate>
                                <asp:Label runat="server" ID="lblCusId" Text='<%# Eval("CustomerId") %>'    ></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:BoundField DataField="CustName" HeaderText="Customer Name" NullDisplayText="This field is required." />
                          


                              <asp:TemplateField>
                                <HeaderTemplate>
                                    Product Id
                                </HeaderTemplate>
                                <ItemTemplate>
                                <asp:Label runat="server" ID="lblPId" Text='<%# Eval("ProductId") %>' ></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>


                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Product Name
                                </HeaderTemplate>
                                <ItemTemplate>
                                      <asp:DropDownList runat="server" ID="drpProd1" AppendDataBoundItems="true" Enabled="false">
                                        <asp:ListItem Value="">--Select--</asp:ListItem>
                                     </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true"
                                CancelText="Cancel" DeleteText="Delete" UpdateText="Update" HeaderText="Action" />
                        </Columns>
                        <EmptyDataTemplate>
                            No Customer & Product details avaliable.</EmptyDataTemplate>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </asp:Panel>
</asp:Content>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) HCL Technologies
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions