我再次卡住了!我正在制作一个销售应用程序,通过选择一些复选框从数据库中提取项目。有一个按钮,我点击后选择某些复选框,并在gridview中获取项目。我有2个未绑定的字段 - “数量”(具有下拉菜单)和“金额”(作为标签)。我选择数量,金额在“金额”列中计算。我还完美地计算了“金额”列的总数。设置未绑定数据字段的前一个gridview数据
当我取消选中某个复选框或想要删除某个项目时,回发事件触发并且该项目被删除,或者该行并排排列,并且前面选定的下拉列表中的值和数量标签也是反过来 - 我的意思是以前的值丢失,因为它把它作为一个全新的查询。
是否有任何方法可以保存以前的值,无论我删除或添加网格中的新项目....?请帮忙!
的ASPX如下:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
OnRowDataBound="GridView1_RowDataBound" ShowFooter="true">
<footerstyle backcolor="LightCyan"
forecolor="MediumBlue"/>
<Columns>
<asp:BoundField DataField="item_ID" HeaderText="ID" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:DropDownList ID="ddlQuantity" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlQuantity_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="purchase_price">
<ItemTemplate>
<asp:Label ID="lblPrice" Text='<%#Eval("purchase_price") %>' runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" Text='<%#Eval("purchase_price") %>' runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
这里是代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
//this is where I am stuck I think so....
//GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow;
//for (int i = 0; i < GridView1.Rows.Count; i++)
//{
// var ddlQuantity = gvr.FindControl("ddlQuantity") as DropDownList;
// Label2.Text = ddlQuantity.SelectedValue.ToString();
//}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
List<String> wheres = new List<String>();
if (CheckBox1.Checked)
{
wheres.Add("'pepsi'");
}
if (CheckBox2.Checked)
{
wheres.Add("'coke'");
}
if (CheckBox3.Checked)
{
wheres.Add("'juice'");
}
String whereclause = String.Join(",", wheres.ToArray());
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=xxx; Initial Catalog=xxx; Integrated Security=True";
conn.Open();
//SqlCommand cmd = new SqlCommand("select * from item_purchase where item_name in ("+whereclause+")", conn);
SqlCommand cmd = new SqlCommand("select item_purchase.purchase_date,item_purchase.purchase_price,item_purchase.item_ID,items.item_name from item_purchase inner join items on items.item_ID = item_purchase.item_ID where items.item_name in (" + whereclause + ") ", conn);
//cmd.Parameters.AddWithValue("@item_names", whereclause);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ViewState["currentTable"] = ds;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = e.Row.FindControl("ddlQuantity") as DropDownList;
if (ddl != null)
{
ddl.DataSource = new List<string>() { "0", "1", "2", "3", "4" };
ddl.DataBind();
}
}
}
protected void ddlQuantity_SelectedIndexChanged(object sender, EventArgs e)
{
int quantity;
GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow;
var ddlQuantity = gvr.FindControl("ddlQuantity") as DropDownList;
var lblAmount = gvr.FindControl("lblAmount") as Label;
var lblPrice = gvr.FindControl("lblPrice") as Label;
int.TryParse(ddlQuantity.SelectedValue, out quantity);
//Label1.Text = quantity.ToString();
int pr = int.Parse(lblPrice.Text);
int qt = int.Parse(ddlQuantity.SelectedValue);
//Label2.Text += (pr*qt).ToString();
lblAmount.Text = (pr * qt).ToString();
int sum = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
gvr = GridView1.Rows[i] as GridViewRow;
var lblrowamnt = gvr.FindControl("lblAmount") as Label;
sum += int.Parse(lblrowamnt.Text);
}
Label2.Text = sum.ToString();
}