datagridview如何自动合计?
开发进销存系统时,利用datagridview手动输入数据,然后最后列有个金额的合计,如何在最后一行添加合计行,并且输入数据时自动更新?求高手帮助!
网上有实例,刚好我也做了,不过翻页还是有点问题!我也希望得到解决.我的是GridView...
贴出来给你看看.
//声明合计总金额和总数量
double totalMoney, totalNum;
//订购单班组明细行梆定的处理事件
protected void gvPurchaseDetailItem_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((DataRowView)e.Row.DataItem)["MaterialID"] == null || (((DataRowView)e.Row.DataItem)["MaterialID"].ToString() == ""))
{
e.Row.Cells[2].Controls[0].Visible = false;
for (int i = 4; i < e.Row.Cells.Count - 1; i++)
{
(e.Row.Cells[i].Controls[0] as TextBox).Text = "";
}
}
else
{
DataRowView drDI = (DataRowView)e.Row.DataItem;
DropDownList ddl = (DropDownList)e.Row.Cells[2].FindControl("Unit");
HiddenField hfUnit = (HiddenField)e.Row.Cells[2].FindControl("hfUnit");
TextBox tb = (TextBox)e.Row.Cells[1].FindControl("MaterialID");
e.Row.Cells[3].Text = bllMaterial.GetLatelyMaterialPriceDetail(this.txtSupplier.Text.Split('-')[0].ToString(), tb.Text.Split('-')[0], DateTime.Now.ToString("yyyy-MM-dd")).Rows[0]["Price"].ToString();
e.Row.Cells[3].Text = Convert.ToDecimal(e.Row.Cells[3].Text).ToString("0.000");
if (ddl != null && tb.Text.Split('-')[0] == "" && tb.Text.Split('-')[0] != " ")
{
ddl.DataSource = bllMaterial.GetMaterialUnitByMaterialID(tb.Text.Split('-')[0]);
ddl.DataTextField = "UnitName";
ddl.DataValueField = "UnitID";
ddl.DataBind();
}
Material item = bllMaterial.GetMaterialByMaterialID(tb.Text.Split('-')[0]);
if (!item.Spec.Equals(""))
{
tb.Text = item.MaterialID + "-" + item.MaterialName + "-" + item.Spec;
}
else
{
tb.Text = item.MaterialID + "-" + item.MaterialName;
}
ddl.SelectedValue = drDI["Unit"].ToString();
hfUnit.Value = drDI["Unit"].ToString();
TextBox tbQty = (TextBox)e.Row.Cells[e.Row.Cells.Count - 1].FindControl("Qty");
e.Row.Cells[e.Row.Cells.Count - 1].Text = (Convert.ToDecimal(e.Row.Cells[3].Text) * Convert.ToDecimal(tbQty.Text)).ToString();
e.Row.Cells[e.Row.Cells.Count - 1].Text = Convert.ToDecimal(e.Row.Cells[e.Row.Cells.Count - 1].Text).ToString("0.0000");
totalMoney += Convert.ToDouble(e.Row.Cells[e.Row.Cells.Count - 1].Text);
totalNum += Convert.ToDouble(tbQty.Text);
}
//绑定Ajax控件参数
((AutoCompleteExtender)e.Row.Cells[1].Controls[1]).ContextKey = txtSupplier.Text.Split('-')[0];
if (hfdStates.Value.Equals(OperationState.VIEW.ToString()))
{
(e.Row.Cells[1].Controls[0] as TextBox).ReadOnly = true;
(e.Row.Cells[2].Controls[0] as DropDownList).Enabled = false;
TextBox tb = (TextBox)e.Row.Cells[1].FindControl("MaterialID");
if (tb.Text == "" || tb.Text == null)
{
(e.Row.Cells[3].Controls[0] as TextBox).ReadOnly = true;
}
for (int i = 4; i < e.Row.Cells.Count - 1; i++)
{
(e.Row.Cells[i].Controls[0] as TextBox).ReadOnly = true;
}
}
else
{
(e.Row.Cells[1].Controls[0] as TextBox).ReadOnly = false;
(e.Row.Cells[2].Controls[0] as DropDownList).Enabled = true;
TextBox tb = (TextBox)e.Row.Cells[1].FindControl("MaterialID");
if (tb.Text == "" || tb.Text == null)
{
e.Row.Cells[3].Controls[0].Visible = true;
}
for (int i = 4; i < e.Row.Cells.Count - 1; i++)
{
(e.Row.Cells[i].Controls[0] as TextBox).ReadOnly = false;
}
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[e.Row.Cells.Count - 3].Text = "<B>合计<B>";
e.Row.Cells[e.Row.Cells.Count - 3].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[e.Row.Cells.Count - 2].Text = string.Format("{0}", totalNum);
e.Row.Cells[e.Row.Cells.Count - 2].Text = "<B>" + Convert.ToDecimal(e.Row.Cells[e.Row.Cells.Count - 2].Text).ToString("0.0000") + "<B>";
e.Row.Cells[e.Row.Cells.Count - 1].Text = string.Format("{0}", totalMoney);
e.Row.Cells[e.Row.Cells.Count - 1].Text = "<B>" + Convert.ToDecimal(e.Row.Cells[e.Row.Cells.Count - 1].Text).ToString("0.0000") + "<B>";
}
}
呵呵,里面东西很杂,不晓得你是不是看晕了...嘿嘿...