| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2462 人关注过本帖
标题:datagridview如何自动合计?
只看楼主 加入收藏
wcbts520
Rank: 1
等 级:新手上路
帖 子:86
专家分:7
注 册:2007-10-11
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:3 
datagridview如何自动合计?
开发进销存系统时,利用datagridview手动输入数据,然后最后列有个金额的合计,如何在最后一行添加合计行,并且输入数据时自动更新?求高手帮助!
搜索更多相关主题的帖子: 进销存系统 
2010-12-23 11:41
筱晓绾
Rank: 10Rank: 10Rank: 10
来 自:湖南
等 级:贵宾
威 望:12
帖 子:512
专家分:1736
注 册:2010-9-1
收藏
得分:10 
网上有实例,刚好我也做了,不过翻页还是有点问题!我也希望得到解决.我的是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] != "&nbsp;")
                    {
                        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>";
            }
        }
呵呵,里面东西很杂,不晓得你是不是看晕了...嘿嘿...
2010-12-23 12:04
wcbts520
Rank: 1
等 级:新手上路
帖 子:86
专家分:7
注 册:2007-10-11
收藏
得分:0 
感谢,非常感谢!真是及时雨啊!
2010-12-23 12:29
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:10 
XtraGrid提供了类似的功能

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-23 16:11
快速回复:datagridview如何自动合计?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023096 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved