List<VoucherDetailItem> voucher = new List<VoucherDetailItem>();
Dictionary<int, decimal> dict = (Dictionary<int, decimal>)ViewState["products"];
//查询所有产品信息。
foreach (GridViewRow row in this.gvProduct.Rows)
{
CheckBox cbx = (CheckBox)row.Cells[0].FindControl("cbxChoose");
//判断是否为用户选择的产品。
if (cbx.Checked)
{
//判断是否包含已选产品。
if (!dict.ContainsKey(row.DataItemIndex))
{
dict.Add(row.DataItemIndex, 0m);
}
DataKey dataKey = this.gvProduct.DataKeys[row.DataItemIndex];
VoucherDetailItem voucherItem = new VoucherDetailItem();
voucherItem.F_GUID = Guid.Parse(dataKey["F_GUID"].ToString());
voucherItem.F_GoodsName = dataKey["F_GoodsName"].ToString();
voucherItem.F_TypeName = dataKey["F_TypeName"].ToString();
voucherItem.F_SuppliersGoodsGUID = Guid.Parse(dataKey["F_SuppliersGoodsGUID"].ToString());
voucherItem.F_Count = Decimal.Parse(dataKey["F_Count"].ToString());
voucherItem.fCount = dict[row.DataItemIndex];
voucher.Add(voucherItem);
}
else
{
if (dict.ContainsKey(row.DataItemIndex))
{
dict.Remove(row.DataItemIndex);
}
}
}
//出库数量和出库的金额。
this.gvVoucher.DataKeyNames = new string[] { "F_GoodsName", "F_Guid", "F_SuppliersGoodsGUID", "F_Count", "F_TypeName", "fCount" };
this.gvVoucher.DataSource = voucher;
this.gvVoucher.DataBind();
private void refreshViewState()
{
Dictionary<int, decimal> dict = (Dictionary<int, decimal>)ViewState["products"];
foreach (GridViewRow row in this.gvVoucher.Rows)
{
KeyValuePair<int, decimal> item = dict.ElementAt<KeyValuePair<int, decimal>>(row.DataItemIndex);
TextBox tbx = (TextBox)row.Cells[6].FindControl("txtCount");
decimal result = 0m;
if (Decimal.TryParse(tbx.Text, out result))
{
dict[item.Key] = result;
}
}
}