hi
I use gridview in page that use session to fill it below are code:
<asp:GridView runat="server" ID="gvOrders" EmptyDataText="No orderPlaced !" ShowFooter="true" CssClass="gridorder"
AutoGenerateColumns="false" DataKeyNames="Id">
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:BoundField DataField="Type" HeaderText="Type" />
<asp:BoundField DataField="Name" HeaderText="Film Name" />
<asp:BoundField DataField="Quality" HeaderText="Quality" />
<asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:C}" />
<asp:BoundField DataField="PriceT" HeaderText="PriceT" DataFormatString="{0:C}" />
<asp:BoundField DataField="Quanyity" HeaderText="Quanyity" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRemove" Text="Remove" runat="server" OnClick="Remove" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and behind code:
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;
}
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();
string Code = dtFiles.Rows[0][4].ToString();
string Type = dtFiles.Rows[0][36].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)),new DataColumn("code",typeof(string)),new DataColumn("Type",typeof(string)), });
}
if (RBmkv.Checked || RBmkv.Checked)
{
if (RBmkv.Checked)
{
DataRow duplicate = (from d in dt.AsEnumerable()
where d.Field<string>("Quality") == RBmkv.Text.Trim()
&& 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, RBmkv.Text.Trim(), PostMkvPr, PostMkvPr, MKVE,Code,Type);
}
}
if (RBdvd.Checked)
{
DataRow duplicate = (from d in dt.AsEnumerable()
where d.Field<string>("Quality") == RBdvd.Text.Trim() && 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, RBdvd.Text.Trim(),PostDvdPr,PostDvdPr, DVDE,Code,Type);
}
}
Session["Order"] = dt;
}
BindOrder();
}
protected void Remove(object sender, EventArgs e)
{
if (Session["Order"] != null)
{
DataTable dtOrder = Session["Order"] as DataTable;
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
int id = Convert.ToInt32(gvOrders.DataKeys[row.RowIndex].Value);
DataRow orderRow = dtOrder.Select().Single(x => Convert.ToInt32(x["Id"]) == (id));
dtOrder.Rows.Remove(orderRow);
dtOrder.AcceptChanges();
Session["Order"] = dtOrder;
BindOrder();
}
}
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>("Price"));
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();
}
}
}
when I click on Imgorder_Click it will put data in session and call Bindorder() metod that will bind gridview, result will be like:
as I show with red arrow here it will show Quantity of order that I define in Imgorder_Click() metod in this line:
if (RBmkv.Checked)
{
DataRow duplicate = (from d in dt.AsEnumerable()
where d.Field<string>("Quality") == RBmkv.Text.Trim()
&& 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, RBmkv.Text.Trim(), PostMkvPr, PostMkvPr, MKVE,Code,Type);
}
quantity is in databound in gridview and it will show quantity of users order...
now I want in quantity column in gridview I can put Dropdownlist that will fill with numbers that if users want increase their order they can do it from gridview...
how I can do it?
best regards
neda