ASP DOT NET

Saturday, June 9, 2018

Q.How to bind drop down with grid view in asp.net c#

.ASPX

<form id="form1" runat="server">
        <div align="center">
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                Width="492px" OnPageIndexChanging="GridView1_PageIndexChanging"
                ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" />
                    <asp:BoundField DataField="ProductName" HeaderText="Name" />
                    <asp:BoundField DataField="UnitPrice" HeaderText="Price/Unit" DataFormatString="{0:F}" />

                    <asp:TemplateField HeaderText="Qty">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" onchange="showData(this)">
                                <asp:ListItem Selected="True" Text="1" Value="1"></asp:ListItem>
                                <asp:ListItem Text="2" Value="2"></asp:ListItem>
                                <asp:ListItem Text="3" Value="3"></asp:ListItem>
                                <asp:ListItem Text="4" Value="4"></asp:ListItem>
                                <asp:ListItem Text="5" Value="5"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="lblTotalPrice" runat="server" Text="Grand Total"></asp:Label>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Price" ItemStyle-HorizontalAlign="Right">
                        <ItemTemplate>
                            <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("UnitPrice", "{0:F}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>

.ASPX.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UploadAndDownload
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        decimal total;
        protected void Page_Load(object sender, EventArgs e)
        {
           // NorthWindEntities container = new NorthWindEntities();
            if (!IsPostBack)
            {
                using (var container = new NorthWindEntities())
                {
                    GridView1.DataSource = container.Products.ToList();
                    GridView1.DataBind();
                }
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            using (var container = new NorthWindEntities())
            {
                GridView1.PageIndex = e.NewPageIndex;
                GridView1.DataSource = container.Products.ToList();
                GridView1.DataBind();
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //calculate the grand total here
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));

            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[4].Text = total.ToString("#,##0.00");
            }
        }
    }

}





No comments:

Post a Comment

How to to select duplicate rows from sql server?

 SELECT * FROM Recruitment WHERE Email IN (SELECT Email FROM Recruitment GROUP BY Email HAVING COUNT(*) > 1); WITH CTE AS (     SELECT   ...