Fisrt of all create class model
public class ProductMake
{
public int ProductMakeID { get; set; }
public string Make { get; set; }
}
public class ProductModel
{
public int ProductModelID { get; set; }
public int ProductMakeID { get; set; }
public string Model { get; set; }
public decimal Price { get; set; }
}
public class ProductOrder
{
public int ProductOrderID { get; set; }
public int ProductMakeID { get; set; }
public int ProductModelID { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public short Quantity { get; set; }
public decimal Price { get; set; }
}
ASPX
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false" DataKeyNames="ProductOrderID" OnRowCancelingEdit="gvProducts_RowCancelingEdit"
OnRowEditing="gvProducts_RowEditing" OnRowDataBound="gvProducts_RowDataBound" >
<Columns>
<asp:BoundField DataField="ProductOrderID" ReadOnly="true" />
<asp:TemplateField HeaderText="Make">
<ItemTemplate>
<asp:Label ID="lblMake" runat="server" Text='<%# Bind("Make") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlProductMake" runat="server"
OnSelectedIndexChanged="ddlProductMake_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model">
<ItemTemplate>
<asp:Label ID="lblModel" runat="server" Text='<%# Bind("Model") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlProductModel" runat="server"
OnSelectedIndexChanged="ddlProductModel_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="True" DataFormatString="{0:c}" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
ASPX.CS
using Business_Access_Layer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Aqua_Demo.ASPX
{
public partial class DropDownInSideGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private List<ProductOrder> GetUserProductOrder()
{
List<ProductOrder> orders = new List<ProductOrder>();
ProductOrder po = new ProductOrder();
po.ProductOrderID = 1;
po.ProductMakeID = 1;
po.ProductModelID = 1;
po.Make = "Apple";
po.Model = "iPhone 4";
po.Quantity = 2;
po.Price = 499;
orders.Add(po);
po = new ProductOrder();
po.ProductOrderID = 2;
po.ProductMakeID = 2;
po.ProductModelID = 4;
po.Make = "Samsung";
po.Model = "Galaxy S2";
po.Quantity = 1;
po.Price = 449;
orders.Add(po);
po = new ProductOrder();
po.ProductOrderID = 3;
po.ProductMakeID = 3;
po.ProductModelID = 7;
po.Make = "Nokia";
po.Model = "Lumia";
po.Quantity = 1;
po.Price = 549;
orders.Add(po);
return orders;
}
private List<ProductMake> GetProductMakes()
{
List<ProductMake> products = new List<ProductMake>();
ProductMake p = new ProductMake();
p.ProductMakeID = 1;
p.Make = "Apple";
products.Add(p);
p = new ProductMake();
p.ProductMakeID = 2;
p.Make = "Samsung";
products.Add(p);
p = new ProductMake();
p.ProductMakeID = 3;
p.Make = "Nokia";
products.Add(p);
return products;
}
private List<ProductModel> GetProductModels()
{
List<ProductModel> productModels = new List<ProductModel>();
ProductModel pm = new ProductModel();
pm.ProductMakeID = 1;
pm.ProductModelID = 1;
pm.Model = "iPhone 4";
pm.Price = 499;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 1;
pm.ProductModelID = 2;
pm.Model = "iPhone 4s";
pm.Price = 599;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 1;
pm.ProductModelID = 3;
pm.Model = "iPhone 5";
pm.Price = 699;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 2;
pm.ProductModelID = 4;
pm.Model = "Galaxy S2";
pm.Price = 449;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 2;
pm.ProductModelID = 5;
pm.Model = "Galaxy S3";
pm.Price = 549;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 2;
pm.ProductModelID = 6;
pm.Model = "Galaxy Note2";
pm.Price = 619;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 3;
pm.ProductModelID = 7;
pm.Model = "Nokia Lumia";
pm.Price = 659;
productModels.Add(pm);
return productModels;
}
private List<ProductModel> GetProductModelByMake(int productMakeID)
{
var models = (from p in GetProductModels()
where p.ProductMakeID == productMakeID
select p);
return models.ToList();
}
private void BindGrid()
{
gvProducts.DataSource = GetUserProductOrder();
gvProducts.DataBind();
}
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
{
gvProducts.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void gvProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvProducts.EditIndex = -1;
BindGrid();
}
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlMake = (DropDownList)e.Row.FindControl("ddlProductMake");
ddlMake.DataSource = GetProductMakes();
ddlMake.DataValueField = "ProductMakeID";
ddlMake.DataTextField = "Make";
ddlMake.DataBind();
ddlMake.SelectedValue = gvProducts.DataKeys[e.Row.RowIndex].Value.ToString();
DropDownList ddlModel = (DropDownList)e.Row.FindControl("ddlProductModel");
ddlModel.DataSource = GetProductModelByMake(Convert.ToInt32(gvProducts.DataKeys[e.Row.RowIndex].Value));
ddlModel.DataValueField = "ProductModelID";
ddlModel.DataTextField = "Model";
ddlModel.DataBind();
ddlModel.SelectedValue = GetProductModelByMake(Convert.ToInt32(gvProducts.DataKeys[e.Row.RowIndex].Value))
.FirstOrDefault().ProductModelID.ToString();
}
}
}
protected void ddlProductMake_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlMake = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlMake.NamingContainer;
if (row != null)
{
if ((row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlModel = (DropDownList)row.FindControl("ddlProductModel");
ddlModel.DataSource = GetProductModelByMake(Convert.ToInt32(ddlMake.SelectedValue));
ddlModel.DataValueField = "ProductModelID";
ddlModel.DataTextField = "Model";
ddlModel.DataBind();
}
}
}
protected void ddlProductModel_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlModel = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlModel.NamingContainer;
if (row != null)
{
if ((row.RowState & DataControlRowState.Edit) > 0)
{
row.Cells[3].Text = string.Format("{0:C}", GetProductModels()
.Where(o => o.ProductModelID == Convert.ToInt32(ddlModel.SelectedValue))
.FirstOrDefault().Price);
}
}
}
}
}
public class ProductMake
{
public int ProductMakeID { get; set; }
public string Make { get; set; }
}
public class ProductModel
{
public int ProductModelID { get; set; }
public int ProductMakeID { get; set; }
public string Model { get; set; }
public decimal Price { get; set; }
}
public class ProductOrder
{
public int ProductOrderID { get; set; }
public int ProductMakeID { get; set; }
public int ProductModelID { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public short Quantity { get; set; }
public decimal Price { get; set; }
}
ASPX
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false" DataKeyNames="ProductOrderID" OnRowCancelingEdit="gvProducts_RowCancelingEdit"
OnRowEditing="gvProducts_RowEditing" OnRowDataBound="gvProducts_RowDataBound" >
<Columns>
<asp:BoundField DataField="ProductOrderID" ReadOnly="true" />
<asp:TemplateField HeaderText="Make">
<ItemTemplate>
<asp:Label ID="lblMake" runat="server" Text='<%# Bind("Make") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlProductMake" runat="server"
OnSelectedIndexChanged="ddlProductMake_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model">
<ItemTemplate>
<asp:Label ID="lblModel" runat="server" Text='<%# Bind("Model") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlProductModel" runat="server"
OnSelectedIndexChanged="ddlProductModel_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="True" DataFormatString="{0:c}" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
ASPX.CS
using Business_Access_Layer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Aqua_Demo.ASPX
{
public partial class DropDownInSideGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private List<ProductOrder> GetUserProductOrder()
{
List<ProductOrder> orders = new List<ProductOrder>();
ProductOrder po = new ProductOrder();
po.ProductOrderID = 1;
po.ProductMakeID = 1;
po.ProductModelID = 1;
po.Make = "Apple";
po.Model = "iPhone 4";
po.Quantity = 2;
po.Price = 499;
orders.Add(po);
po = new ProductOrder();
po.ProductOrderID = 2;
po.ProductMakeID = 2;
po.ProductModelID = 4;
po.Make = "Samsung";
po.Model = "Galaxy S2";
po.Quantity = 1;
po.Price = 449;
orders.Add(po);
po = new ProductOrder();
po.ProductOrderID = 3;
po.ProductMakeID = 3;
po.ProductModelID = 7;
po.Make = "Nokia";
po.Model = "Lumia";
po.Quantity = 1;
po.Price = 549;
orders.Add(po);
return orders;
}
private List<ProductMake> GetProductMakes()
{
List<ProductMake> products = new List<ProductMake>();
ProductMake p = new ProductMake();
p.ProductMakeID = 1;
p.Make = "Apple";
products.Add(p);
p = new ProductMake();
p.ProductMakeID = 2;
p.Make = "Samsung";
products.Add(p);
p = new ProductMake();
p.ProductMakeID = 3;
p.Make = "Nokia";
products.Add(p);
return products;
}
private List<ProductModel> GetProductModels()
{
List<ProductModel> productModels = new List<ProductModel>();
ProductModel pm = new ProductModel();
pm.ProductMakeID = 1;
pm.ProductModelID = 1;
pm.Model = "iPhone 4";
pm.Price = 499;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 1;
pm.ProductModelID = 2;
pm.Model = "iPhone 4s";
pm.Price = 599;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 1;
pm.ProductModelID = 3;
pm.Model = "iPhone 5";
pm.Price = 699;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 2;
pm.ProductModelID = 4;
pm.Model = "Galaxy S2";
pm.Price = 449;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 2;
pm.ProductModelID = 5;
pm.Model = "Galaxy S3";
pm.Price = 549;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 2;
pm.ProductModelID = 6;
pm.Model = "Galaxy Note2";
pm.Price = 619;
productModels.Add(pm);
pm = new ProductModel();
pm.ProductMakeID = 3;
pm.ProductModelID = 7;
pm.Model = "Nokia Lumia";
pm.Price = 659;
productModels.Add(pm);
return productModels;
}
private List<ProductModel> GetProductModelByMake(int productMakeID)
{
var models = (from p in GetProductModels()
where p.ProductMakeID == productMakeID
select p);
return models.ToList();
}
private void BindGrid()
{
gvProducts.DataSource = GetUserProductOrder();
gvProducts.DataBind();
}
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
{
gvProducts.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void gvProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvProducts.EditIndex = -1;
BindGrid();
}
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlMake = (DropDownList)e.Row.FindControl("ddlProductMake");
ddlMake.DataSource = GetProductMakes();
ddlMake.DataValueField = "ProductMakeID";
ddlMake.DataTextField = "Make";
ddlMake.DataBind();
ddlMake.SelectedValue = gvProducts.DataKeys[e.Row.RowIndex].Value.ToString();
DropDownList ddlModel = (DropDownList)e.Row.FindControl("ddlProductModel");
ddlModel.DataSource = GetProductModelByMake(Convert.ToInt32(gvProducts.DataKeys[e.Row.RowIndex].Value));
ddlModel.DataValueField = "ProductModelID";
ddlModel.DataTextField = "Model";
ddlModel.DataBind();
ddlModel.SelectedValue = GetProductModelByMake(Convert.ToInt32(gvProducts.DataKeys[e.Row.RowIndex].Value))
.FirstOrDefault().ProductModelID.ToString();
}
}
}
protected void ddlProductMake_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlMake = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlMake.NamingContainer;
if (row != null)
{
if ((row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlModel = (DropDownList)row.FindControl("ddlProductModel");
ddlModel.DataSource = GetProductModelByMake(Convert.ToInt32(ddlMake.SelectedValue));
ddlModel.DataValueField = "ProductModelID";
ddlModel.DataTextField = "Model";
ddlModel.DataBind();
}
}
}
protected void ddlProductModel_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlModel = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlModel.NamingContainer;
if (row != null)
{
if ((row.RowState & DataControlRowState.Edit) > 0)
{
row.Cells[3].Text = string.Format("{0:C}", GetProductModels()
.Where(o => o.ProductModelID == Convert.ToInt32(ddlModel.SelectedValue))
.FirstOrDefault().Price);
}
}
}
}
}
No comments:
Post a Comment