hi
I have gridview and button and radiobutton in page below are codes...
<input type="radio" name="n" id="RBmkvM" runat="server"/>
<input type="radio" name="n" id="RBdvdM" runat="server"/>
<asp:ImageButton ID="ImageButton1" runat="server" CssClass="imgored" ImageUrl="~/Image/Main/png1.png" OnClick="Imgorder_Click"></asp:ImageButton>
<asp:GridView runat="server" ID="GridView1" EmptyDataText="No orderPlaced !" CssClass="gridorder"
AutoGenerateColumns="false" DataKeyNames="Id" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Film Name" />
<asp:BoundField DataField="Quality" HeaderText="Quality" />
<asp:BoundField DataField="PriceT" HeaderText="PriceT" DataFormatString="{0:0,0}" />
<asp:BoundField DataField="Quanyity" HeaderText="Quanyity" />
<asp:TemplateField ItemStyle-Width="60px" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Label ID="Lblname" runat="server" Text='<%# Eval("Quanyity")%>'></asp:Label>
<asp:DropDownList ID="ddlQuantity" runat="server" OnSelectedIndexChanged="DdlQuantityS">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
AND
protected void Imgorder_Click(object sender, EventArgs e)
{
ImageButton ibtn = sender as ImageButton;
int id = Convert.ToInt32(Request.QueryString["Id"].ToString());
DataTable dtFiles = GetFilmInfo(id);
string Name = dtFiles.Rows[0][1].ToString();
string MKVE = dtFiles.Rows[0][37].ToString();
string DVDE = dtFiles.Rows[0][38].ToString();
string PostMkvPr = dtFiles.Rows[0][12].ToString();
string PostDvdPr = dtFiles.Rows[0][13].ToString();
DataTable dt = new DataTable();
if (Session["Order"] != null)
{
dt = Session["Order"] as DataTable;
}
else
{
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id",typeof(int)),new DataColumn("Name",typeof(string)),new DataColumn("Quality",typeof(string)),
new DataColumn("PriceT", typeof(decimal)), new DataColumn("Price", typeof(decimal)), new DataColumn("Quanyity", typeof(int))});
}
if (RBmkv.Checked || RBdvd.Checked)
{
if (RBmkv.Checked)
{
DataRow duplicate = (from d in dt.AsEnumerable()
where d.Field<string>("Quality") == "MKV"
&& d.Field<string>("Name") == Name
select d).FirstOrDefault();
if (duplicate != null)
{
int qty = Convert.ToInt32(duplicate.ItemArray[5]);
duplicate["Quanyity"] = (qty + 1);
duplicate["PriceT"] = (int.Parse(PostMkvPr) * (qty + 1));
dt.AcceptChanges();
}
else
{
dt.Rows.Add(dt.Rows.Count + 1, Name, "MKV", PostMkvPr, PostMkvPr, MKVE, Code, Type);
}
}
if (RBdvd.Checked)
{
DataRow duplicate = (from d in dt.AsEnumerable()
where d.Field<string>("Quality") == "DVD" && d.Field<string>("Name") == Name
select d).FirstOrDefault();
if (duplicate != null)
{
int qty = Convert.ToInt32(duplicate.ItemArray[5]);
duplicate["Quanyity"] = (qty + 1);
duplicate["PriceT"] = (int.Parse(PostDvdPr) * (qty + 1));
dt.AcceptChanges();
}
else
{
dt.Rows.Add(dt.Rows.Count + 1, Name, "DVD", PostDvdPr, PostDvdPr, DVDE, Code, Type);
}
}
int totalOrder = dt.AsEnumerable().Sum(row => row.Field<int>("Quanyity"));
Session["Order"] = dt;
}
private void BindOrder()
{
if (Session["Order"] != null)
{
DataTable dt = Session["Order"] as DataTable;
gvOrders.DataSource = dt;
gvOrders.DataBind();
if (dt.Rows.Count > 0)
{
decimal totalPrice = dt.AsEnumerable().Sum(row => row.Field<decimal>("PriceT"));
gvOrders.FooterRow.Cells[0].Text = "Total";
gvOrders.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;
gvOrders.FooterRow.Cells[2].Text = "تومان" + totalPrice.ToString("N2");
int totalOrder = dt.AsEnumerable().Sum(row => row.Field<int>("Quanyity"));
gvOrders.FooterRow.Cells[3].Text = totalOrder.ToString();
Label2.Text= totalPrice.ToString();
}
}
}
private DataTable GetFilmInfo(int id)
{
DataTable dt = new DataTable();
using (SqlConnection conn = General.GetConnection())
{
using (SqlCommand cmd = General.GetCommand("Documentry_orderInfo", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
return dt;
}
here I users should select one radiobutton and according to their selection it will make session and save users ordered information in sesstion and will show in gridview according to below code:
if (RBdvdM.Checked)
{
DataRow duplicate = (from d in dt.AsEnumerable()
where d.Field<string>("Quality") == "DVD" && d.Field<string>("Name") == Name
select d).FirstOrDefault();
if (duplicate != null)
{
int qty = Convert.ToInt32(duplicate.ItemArray[5]);
duplicate["Quanyity"] = (qty + 1);
duplicate["PriceT"] = (int.Parse(MPostDvdPr) * (qty + 1));
dt.AcceptChanges();
}
if they select one radio button two time it will increase Quantity value
and bind gridview now I put DropDown list in gridview and bind it from database that include numbers 1-10
<asp:GridView runat="server" ID="GridView1" EmptyDataText="No orderPlaced !" CssClass="gridorder"
AutoGenerateColumns="false" DataKeyNames="Id" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Film Name" />
<asp:BoundField DataField="Quality" HeaderText="Quality" />
<asp:BoundField DataField="PriceT" HeaderText="PriceT" DataFormatString="{0:0,0}" />
<asp:BoundField DataField="Quanyity" HeaderText="Quanyity" />
<asp:TemplateField ItemStyle-Width="60px" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Label ID="Lblname" runat="server" Text='<%# Eval("Quanyity")%>'></asp:Label>
<asp:DropDownList ID="ddlQuantity" runat="server" OnSelectedIndexChanged="DdlQuantityS">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
using (SqlConnection conn = General.GetConnection())
{
using (SqlCommand cmd = General.GetCommand("OrderNum", conn))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlQuantity = (e.Row.FindControl("ddlQuantity") as DropDownList);
ddlQuantity.DataSource = GetData(cmd);
ddlQuantity.DataTextField = "number";
ddlQuantity.DataValueField = "number";
ddlQuantity.DataBind();
string country = (e.Row.FindControl("Lblname") as Label).Text;
ddlQuantity.Items.FindByValue(country).Selected = true;
}
}
}
}
private DataTable GetData(SqlCommand cmd)
{
using (SqlConnection conn = General.GetConnection())
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
conn.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
}
now I want if users want increase quantity of their orders they can select number from dropdownlist and see result in gridview(I mean if they select 2 from Dropdownlist it will increase price in gridview)
I wrote below code but it is not correct:
<asp:DropDownList ID="ddlQuantity" runat="server" OnSelectedIndexChanged="DdlQuantityS">
protected void DdlQuantityS(object sender, EventArgs e)
{
DataTable dt = Session["Order"] as DataTable;
DataRow duplicate = (from d in dt.AsEnumerable()
where d.Field<string>("Quality") == "MKV"
&& d.Field<string>("Name") == ("Name")
select d).FirstOrDefault();
if (duplicate != null)
{
int qty = Convert.ToInt32(duplicate.ItemArray[5]);
duplicate["Quanyity"] = (qty + 1);
duplicate["PriceT"] = (int.Parse("MPostMkvPr") * (qty + 1));
dt.AcceptChanges();
}
BindOrder();
}
how I can do it?
Best regards
Neda