首页 > 编程语言 >c#、winform数据统计,需要统计三四张表的数据,在一个界面上显示

c#、winform数据统计,需要统计三四张表的数据,在一个界面上显示

时间:2023-08-17 16:34:12浏览次数:37  
标签:期初 Rows string c# decimal Convert winform 统计 dtt

前几天我就遇到了这个需求,我之前实习写过.noodjs,处理过一些这样的需求,不过那时候我很菜,都是SQL大法整出来的,

但是SqlServer就没有MySQL那么好用了,sql写复杂了对效率和稳定性会有不小的影响

我现在的需求就是我先截一张图,给各位看看,需求是什么

四张表,一个表未基本表,另外三张表分别根据不同的时间段去统计

所以在代码上我是,四哥个简单查询sql,然后四个datatable将他们放进去,通过四张表的主外键串起来,最后塞到datagridview中

 

第一步先将期初,本期的时间写出来,以便后面进行where,期初:为上月的最后一天,本期未本月,本月我用的datatimepiker控件,

付代码:

string yue = dateTimePicker1.Text.Trim();//拿到datatimepiker控件的值

string tian = Convert.ToDateTime(yue).AddMonths(0).AddDays(-1).ToString("yyyy-MM-dd");//得到上个月最后一天

几个sql,这里的我将所有的sql塞到datatable中

//即时库存
            string ss = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位  from MaterialStock";
            da = new SqlDataAdapter(ss, SQL);
            dt = new DataTable();
            da.Fill(dt);

 

//期初入库
            string str = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from PutStockDetial where date like '%" + tian + "%' group by materialsId,materialsName,specification,unit";
            da1 = new SqlDataAdapter(str, SQL);
            dt1 = new DataTable();
            da1.Fill(dt1);

            //期初出库
            string str1 = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from OutStockDetial where date like '%" + tian + "%' group by materialsId,materialsName,specification,unit";
            da2 = new SqlDataAdapter(str1, SQL);
            dt2 = new DataTable();
            da2.Fill(dt2);

            //本期收入
            string str2 = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from PutStockDetial where date like '%" + yue + "%' group by materialsId,materialsName,specification,unit";
            da3 = new SqlDataAdapter(str2, SQL);
            dt3 = new DataTable();
            da3.Fill(dt3);

            //本期发出
            string str3 = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from OutStockDetial where date like '%" + yue + "%' group by materialsId,materialsName,specification,unit";
            da4 = new SqlDataAdapter(str3, SQL);
            dt4 = new DataTable();
            da4.Fill(dt4);

 

然后将在定义一个datatable,将以上所有datatable中的所需的列全部塞到新的datatable中,未定义的新的datatable为dtt

//添加列名称
            DataTable dtt = new DataTable();
            dtt.Columns.Add("时间", typeof(string));
            dtt.Columns.Add("物料编码", typeof(string));
            dtt.Columns.Add("物料名称", typeof(string));
            dtt.Columns.Add("规格型号", typeof(string));
            dtt.Columns.Add("单位", typeof(string));

            dtt.Columns.Add("期初收入数量", typeof(int));
            dtt.Columns.Add("期初收入单价", typeof(decimal));
            dtt.Columns.Add("期初收入金额", typeof(decimal));

            dtt.Columns.Add("期初发出数量", typeof(int));
            dtt.Columns.Add("期初发出单价", typeof(decimal));
            dtt.Columns.Add("期初发出金额", typeof(decimal));

            dtt.Columns.Add("期初结存数量", typeof(int));
            dtt.Columns.Add("期初结存单价", typeof(decimal));
            dtt.Columns.Add("期初结存金额", typeof(decimal));

            dtt.Columns.Add("本期收入数量", typeof(int));
            dtt.Columns.Add("本期收入单价", typeof(decimal));
            dtt.Columns.Add("本期收入金额", typeof(decimal));

            dtt.Columns.Add("本期发出数量", typeof(int));
            dtt.Columns.Add("本期发出单价", typeof(decimal));
            dtt.Columns.Add("本期发出金额", typeof(decimal));

            dtt.Columns.Add("期末结存数量", typeof(int));
            dtt.Columns.Add("期末结存单价", typeof(decimal));
            dtt.Columns.Add("期末结存金额", typeof(decimal));

然后循环不同的datatable,将需要的值取出来一次的塞到dtt里面的列中

//库存表
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dtt.NewRow();
                string wlbm = dt.Rows[i]["物料编码"].ToString();
                string wlmc = dt.Rows[i]["物料名称"].ToString();
                string gg = dt.Rows[i]["规格型号"].ToString();
                string dw = dt.Rows[i]["单位"].ToString();
                dr["时间"] = yue;
                dr["物料编码"] = wlbm;
                dr["物料名称"] = wlmc;
                dr["规格型号"] = gg;
                dr["单位"] = dw;
                //期初入
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    id1 = dt1.Rows[j]["物料编码"].ToString();
                    if (wlbm == id1)
                    {
                        slR = Convert.ToInt32(dt1.Rows[j]["数量"]);
                        djR = Convert.ToDecimal(dt1.Rows[j]["单价"]);
                        jeR = Convert.ToDecimal(dt1.Rows[j]["金额"]);
                        dr["期初收入数量"] = slR;
                        dr["期初收入单价"] = djR;
                        dr["期初收入金额"] = jeR;
                    }
                }
                //期初出  
                for (int k = 0; k < dt2.Rows.Count; k++)
                {
                    id2 = dt2.Rows[k]["物料编码"].ToString();
                    if (wlbm == id2)
                    {
                        slC = Convert.ToInt32(dt2.Rows[k]["数量"]);
                        djC = Convert.ToDecimal(dt2.Rows[k]["单价"]);
                        jeC = Convert.ToDecimal(dt2.Rows[k]["金额"]);
                        dr["期初发出数量"] = slC;
                        dr["期初发出单价"] = djC;
                        dr["期初发出金额"] = jeC;
                    }
                }
                //本期收入
                if (dt3.Rows.Count > 0)
                {
                    for (int ii = 0; ii < dt3.Rows.Count; ii++)
                    {
                        if (wlbm == dt3.Rows[ii]["物料编码"].ToString())
                        {
                            sl1 = Convert.ToInt32(dt3.Rows[ii]["数量"]);
                            dj1 = Convert.ToDecimal(dt3.Rows[ii]["单价"]);
                            je1 = Convert.ToDecimal(dt3.Rows[ii]["金额"]);
                            dr["本期收入数量"] = sl1;
                            dr["本期收入单价"] = dj1;
                            dr["本期收入金额"] = je1;
                        }
                        /*else
                        {
                            dr["本期收入数量"] = 0;
                            dr["本期收入单价"] = 0;
                            dr["本期收入金额"] = 0;
                        }*/
                    }
                }
                
                //本期发出
                for (int jj = 0; jj < dt4.Rows.Count; jj++)
                {
                    if (wlbm == dt4.Rows[jj]["物料编码"].ToString())
                    {
                        sl2 = Convert.ToInt32(dt4.Rows[jj]["数量"]);
                        dj2 = Convert.ToDecimal(dt4.Rows[jj]["单价"]);
                        je2 = Convert.ToDecimal(dt4.Rows[jj]["金额"]);
                        dr["本期发出数量"] = sl2;
                        dr["本期发出单价"] = dj2;
                        dr["本期发出金额"] = je2;
                    }
                    /*else
                    {
                        dr["本期发出数量"] = 0;
                        dr["本期发出单价"] = 0;
                        dr["本期发出金额"] = 0;
                    }*/
                }
                //添加行内容
                dtt.Rows.Add(dr);

接下来的部分是数据处理,不同表向减的数据,然后放入到dtt中,但是相减会出现空指针异常,所以这里面就拿到dtt中的部分数据,进行判空,然后将null值赋值为0

if (dtt.Rows.Count > 0)
                {
                    for (int x = 0; x < dtt.Rows.Count; x++)
                    {
                        if (dtt.Rows[x]["期初收入数量"] == DBNull.Value)
                            dtt.Rows[x]["期初收入数量"] = 0;
                        int a = Convert.ToInt32(dtt.Rows[x]["期初收入数量"]);

                        if (dtt.Rows[x]["期初收入单价"] == DBNull.Value)
                            dtt.Rows[x]["期初收入单价"] = 0;
                        decimal b = Convert.ToDecimal(dtt.Rows[x]["期初收入单价"]);

                        if (dtt.Rows[x]["期初收入金额"] == DBNull.Value)
                            dtt.Rows[x]["期初收入金额"] = 0;
                        decimal c = Convert.ToDecimal(dtt.Rows[x]["期初收入金额"]);

                        if (dtt.Rows[x]["期初发出数量"] == DBNull.Value)
                            dtt.Rows[x]["期初发出数量"] = 0;
                        int a1 = Convert.ToInt32(dtt.Rows[x]["期初发出数量"]);

                        if (dtt.Rows[x]["期初发出单价"] == DBNull.Value)
                            dtt.Rows[x]["期初发出单价"] = 0;
                        decimal b1 = Convert.ToDecimal(dtt.Rows[x]["期初发出单价"]);

                        if (dtt.Rows[x]["期初发出金额"] == DBNull.Value)
                            dtt.Rows[x]["期初发出金额"] = 0;
                        decimal c1 = Convert.ToDecimal(dtt.Rows[x]["期初发出金额"]);

                        if (dtt.Rows[x]["本期收入数量"] == DBNull.Value)
                            dtt.Rows[x]["本期收入数量"] = 0;
                        decimal a2 = Convert.ToDecimal(dtt.Rows[x]["本期收入数量"]);

                        if (dtt.Rows[x]["本期收入单价"] == DBNull.Value)
                            dtt.Rows[x]["本期收入单价"] = 0;
                        decimal b2 = Convert.ToDecimal(dtt.Rows[x]["本期收入单价"]);

                        if (dtt.Rows[x]["本期收入金额"] == DBNull.Value)
                            dtt.Rows[x]["本期收入金额"] = 0;
                        decimal c2 = Convert.ToDecimal(dtt.Rows[x]["本期收入金额"]);

                        if (dtt.Rows[x]["本期发出数量"] == DBNull.Value)
                            dtt.Rows[x]["本期发出数量"] = 0;
                        decimal a3 = Convert.ToDecimal(dtt.Rows[x]["本期发出数量"]);

                        if (dtt.Rows[x]["本期发出单价"] == DBNull.Value)
                            dtt.Rows[x]["本期发出单价"] = 0;
                        decimal b3 = Convert.ToDecimal(dtt.Rows[x]["本期发出单价"]);

                        if (dtt.Rows[x]["本期发出金额"] == DBNull.Value)
                            dtt.Rows[x]["本期发出金额"] = 0;
                        decimal c3 = Convert.ToDecimal(dtt.Rows[x]["本期发出金额"]);

                        dtt.Rows[x]["期初结存数量"] = a - a1;
                        dtt.Rows[x]["期初结存单价"] = b - b1;
                        dtt.Rows[x]["期初结存金额"] = c - c1;

                        dtt.Rows[x]["期末结存数量"] = (a2 - a3) + (a - a1);
                        dtt.Rows[x]["期末结存单价"] = (b2 - b3) + (b - b1);
                        dtt.Rows[x]["期末结存金额"] = (c2 - c3) + (c - c1);

                    }
                }

 

接下来将最终处理好的dtt放入datagridview中

dataGridView1.DataSource = dtt;

后面需要对得到的数据进行汇总统计处理,有些不需要显示的列名称就隐藏起来

if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Columns["期初收入数量"].Visible = false;
                dataGridView1.Columns["期初收入单价"].Visible = false;
                dataGridView1.Columns["期初收入金额"].Visible = false;
                dataGridView1.Columns["期初发出数量"].Visible = false;
                dataGridView1.Columns["期初发出单价"].Visible = false;
                dataGridView1.Columns["期初发出金额"].Visible = false;
            }
            int sum0 = 0;
            decimal sum1 = 0;
            decimal sum2 = 0;
            int sum3 = 0;
            decimal sum4 = 0;
            decimal sum5 = 0;
            int sum6 = 0;
            decimal sum7 = 0;
            decimal sum8 = 0;
            int sum9 = 0;
            decimal sum10 = 0;
            decimal sum11 = 0;
            int aa = 0;
            decimal bb = 0;
            decimal cc = 0;
            int dd = 0;
            decimal ee = 0;
            decimal ff = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                aa += Convert.ToInt32(dataGridView1.Rows[i].Cells["期初收入数量"].Value);
                bb += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初收入单价"].Value);
                cc += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初收入金额"].Value);

                dd += Convert.ToInt32(dataGridView1.Rows[i].Cells["期初发出数量"].Value);
                ee += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初发出单价"].Value);
                ff += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初发出金额"].Value);

                sum0 += Convert.ToInt32(dataGridView1.Rows[i].Cells["期初结存数量"].Value);
                sum1 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初结存单价"].Value);
                sum2 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初结存金额"].Value);

                sum3 += Convert.ToInt32(dataGridView1.Rows[i].Cells["本期收入数量"].Value);
                sum4 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期收入单价"].Value);
                sum5 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期收入金额"].Value);

                sum6 += Convert.ToInt32(dataGridView1.Rows[i].Cells["本期发出数量"].Value);
                sum7 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期发出单价"].Value);
                sum8 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期发出金额"].Value);

                sum9 += Convert.ToInt32(dataGridView1.Rows[i].Cells["期末结存数量"].Value);
                sum10 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期末结存单价"].Value);
                sum11 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期末结存金额"].Value);
            }
            string aaa = aa.ToString();
            string bbb = bb.ToString();
            string ccc = cc.ToString();
            string ddd = dd.ToString();
            string eee = ee.ToString();
            string fff = ff.ToString();
            string ssum0 = sum0.ToString();
            string ssum1 = sum1.ToString();
            string ssum2 = sum2.ToString();
            string ssum3 = sum3.ToString();
            string ssum4 = sum4.ToString();
            string ssum5 = sum5.ToString();
            string ssum6 = sum6.ToString();
            string ssum7 = sum7.ToString();
            string ssum8 = sum8.ToString();
            string ssum9 = sum9.ToString();
            string ssum10 = sum10.ToString();
            string ssum11 = sum11.ToString();
            string[] row = { "合计", "","","","",aaa, bbb,ccc,ddd,eee,fff,ssum0 , ssum1 , ssum2 , ssum3 , ssum4 , ssum5 , ssum6 , ssum7 , ssum8 , ssum9 , ssum10 , ssum11 };
            ((DataTable)dataGridView1.DataSource).Rows.Add(row);
        }

完整代码如下:

private static readonly string SQL = ConfigurationManager.AppSettings["connectionstring"];
        SqlDataAdapter da;
        DataTable dt;
        SqlDataAdapter da1;
        DataTable dt1;
        SqlDataAdapter da2;
        DataTable dt2;
        SqlDataAdapter da3;
        DataTable dt3;
        SqlDataAdapter da4;
        DataTable dt4;
        public string con_user { get; set; }
        public string con_group { get; set; }

        private AutoSizeFormClass asc = new AutoSizeFormClass();
        private void InAndPutList_Load(object sender, EventArgs e)
        {
            asc.controllInitializeSize(this);

        }
        private void InAndPutList_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }
        int slR;
        decimal djR;
        decimal jeR;
        List<DataRow> ls = new List<DataRow>();
        int slC;
        decimal djC;
        decimal jeC;

        int sl1;
        decimal dj1;
        decimal je1;

        int sl2;
        decimal dj2;
        decimal je2;

        string id1;
        string id2;
        DataRow dr;

        
        //上月最后一天
        //string sj = DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMonths(0).AddDays(-1).ToString("yyyy-MM-dd");
        //本月时间
        //string shijian = DateTime.Now.ToString("yyyy-MM");
        private void button1_Click(object sender, EventArgs e)
        {
            string yue = dateTimePicker1.Text.Trim();//拿到datatimepiker控件的值

            string tian = Convert.ToDateTime(yue).AddMonths(0).AddDays(-1).ToString("yyyy-MM-dd");//得到上个月最后一天
            Console.WriteLine(yue);
            Console.WriteLine(tian);
            dataGridView1.Columns.Clear();

            //即时库存
            string ss = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位  from MaterialStock";
            da = new SqlDataAdapter(ss, SQL);
            dt = new DataTable();
            da.Fill(dt);

            //期初入库
            string str = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from PutStockDetial where date like '%" + tian + "%' group by materialsId,materialsName,specification,unit";
            da1 = new SqlDataAdapter(str, SQL);
            dt1 = new DataTable();
            da1.Fill(dt1);

            //期初出库
            string str1 = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from OutStockDetial where date like '%" + tian + "%' group by materialsId,materialsName,specification,unit";
            da2 = new SqlDataAdapter(str1, SQL);
            dt2 = new DataTable();
            da2.Fill(dt2);

            //本期收入
            string str2 = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from PutStockDetial where date like '%" + yue + "%' group by materialsId,materialsName,specification,unit";
            da3 = new SqlDataAdapter(str2, SQL);
            dt3 = new DataTable();
            da3.Fill(dt3);

            //本期发出
            string str3 = "select materialsId as 物料编码,materialsName as 物料名称,specification as 规格型号 ,unit as 单位 ,sum(unitNumber) as 数量 ,sum(purchasingPrice) as 单价,sum(stockAmount) as 金额 from OutStockDetial where date like '%" + yue + "%' group by materialsId,materialsName,specification,unit";
            da4 = new SqlDataAdapter(str3, SQL);
            dt4 = new DataTable();
            da4.Fill(dt4);

            //添加列名称
            DataTable dtt = new DataTable();
            dtt.Columns.Add("时间", typeof(string));
            dtt.Columns.Add("物料编码", typeof(string));
            dtt.Columns.Add("物料名称", typeof(string));
            dtt.Columns.Add("规格型号", typeof(string));
            dtt.Columns.Add("单位", typeof(string));

            dtt.Columns.Add("期初收入数量", typeof(int));
            dtt.Columns.Add("期初收入单价", typeof(decimal));
            dtt.Columns.Add("期初收入金额", typeof(decimal));

            dtt.Columns.Add("期初发出数量", typeof(int));
            dtt.Columns.Add("期初发出单价", typeof(decimal));
            dtt.Columns.Add("期初发出金额", typeof(decimal));

            dtt.Columns.Add("期初结存数量", typeof(int));
            dtt.Columns.Add("期初结存单价", typeof(decimal));
            dtt.Columns.Add("期初结存金额", typeof(decimal));

            dtt.Columns.Add("本期收入数量", typeof(int));
            dtt.Columns.Add("本期收入单价", typeof(decimal));
            dtt.Columns.Add("本期收入金额", typeof(decimal));

            dtt.Columns.Add("本期发出数量", typeof(int));
            dtt.Columns.Add("本期发出单价", typeof(decimal));
            dtt.Columns.Add("本期发出金额", typeof(decimal));

            dtt.Columns.Add("期末结存数量", typeof(int));
            dtt.Columns.Add("期末结存单价", typeof(decimal));
            dtt.Columns.Add("期末结存金额", typeof(decimal));
            //添加行内容
            //库存表
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dtt.NewRow();
                string wlbm = dt.Rows[i]["物料编码"].ToString();
                string wlmc = dt.Rows[i]["物料名称"].ToString();
                string gg = dt.Rows[i]["规格型号"].ToString();
                string dw = dt.Rows[i]["单位"].ToString();
                dr["时间"] = yue;
                dr["物料编码"] = wlbm;
                dr["物料名称"] = wlmc;
                dr["规格型号"] = gg;
                dr["单位"] = dw;
                //期初入
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    id1 = dt1.Rows[j]["物料编码"].ToString();
                    if (wlbm == id1)
                    {
                        slR = Convert.ToInt32(dt1.Rows[j]["数量"]);
                        djR = Convert.ToDecimal(dt1.Rows[j]["单价"]);
                        jeR = Convert.ToDecimal(dt1.Rows[j]["金额"]);
                        dr["期初收入数量"] = slR;
                        dr["期初收入单价"] = djR;
                        dr["期初收入金额"] = jeR;
                    }
                }
                //期初出  
                for (int k = 0; k < dt2.Rows.Count; k++)
                {
                    id2 = dt2.Rows[k]["物料编码"].ToString();
                    if (wlbm == id2)
                    {
                        slC = Convert.ToInt32(dt2.Rows[k]["数量"]);
                        djC = Convert.ToDecimal(dt2.Rows[k]["单价"]);
                        jeC = Convert.ToDecimal(dt2.Rows[k]["金额"]);
                        dr["期初发出数量"] = slC;
                        dr["期初发出单价"] = djC;
                        dr["期初发出金额"] = jeC;
                    }
                }
                //本期收入
                if (dt3.Rows.Count > 0)
                {
                    for (int ii = 0; ii < dt3.Rows.Count; ii++)
                    {
                        if (wlbm == dt3.Rows[ii]["物料编码"].ToString())
                        {
                            sl1 = Convert.ToInt32(dt3.Rows[ii]["数量"]);
                            dj1 = Convert.ToDecimal(dt3.Rows[ii]["单价"]);
                            je1 = Convert.ToDecimal(dt3.Rows[ii]["金额"]);
                            dr["本期收入数量"] = sl1;
                            dr["本期收入单价"] = dj1;
                            dr["本期收入金额"] = je1;
                        }
                        /*else
                        {
                            dr["本期收入数量"] = 0;
                            dr["本期收入单价"] = 0;
                            dr["本期收入金额"] = 0;
                        }*/
                    }
                }
                
                //本期发出
                for (int jj = 0; jj < dt4.Rows.Count; jj++)
                {
                    if (wlbm == dt4.Rows[jj]["物料编码"].ToString())
                    {
                        sl2 = Convert.ToInt32(dt4.Rows[jj]["数量"]);
                        dj2 = Convert.ToDecimal(dt4.Rows[jj]["单价"]);
                        je2 = Convert.ToDecimal(dt4.Rows[jj]["金额"]);
                        dr["本期发出数量"] = sl2;
                        dr["本期发出单价"] = dj2;
                        dr["本期发出金额"] = je2;
                    }
                    /*else
                    {
                        dr["本期发出数量"] = 0;
                        dr["本期发出单价"] = 0;
                        dr["本期发出金额"] = 0;
                    }*/
                }
                dtt.Rows.Add(dr);//将行数据塞进去

 

//判空null
                if (dtt.Rows.Count > 0)
                {
                    for (int x = 0; x < dtt.Rows.Count; x++)
                    {
                        if (dtt.Rows[x]["期初收入数量"] == DBNull.Value)
                            dtt.Rows[x]["期初收入数量"] = 0;
                        int a = Convert.ToInt32(dtt.Rows[x]["期初收入数量"]);

                        if (dtt.Rows[x]["期初收入单价"] == DBNull.Value)
                            dtt.Rows[x]["期初收入单价"] = 0;
                        decimal b = Convert.ToDecimal(dtt.Rows[x]["期初收入单价"]);

                        if (dtt.Rows[x]["期初收入金额"] == DBNull.Value)
                            dtt.Rows[x]["期初收入金额"] = 0;
                        decimal c = Convert.ToDecimal(dtt.Rows[x]["期初收入金额"]);

                        if (dtt.Rows[x]["期初发出数量"] == DBNull.Value)
                            dtt.Rows[x]["期初发出数量"] = 0;
                        int a1 = Convert.ToInt32(dtt.Rows[x]["期初发出数量"]);

                        if (dtt.Rows[x]["期初发出单价"] == DBNull.Value)
                            dtt.Rows[x]["期初发出单价"] = 0;
                        decimal b1 = Convert.ToDecimal(dtt.Rows[x]["期初发出单价"]);

                        if (dtt.Rows[x]["期初发出金额"] == DBNull.Value)
                            dtt.Rows[x]["期初发出金额"] = 0;
                        decimal c1 = Convert.ToDecimal(dtt.Rows[x]["期初发出金额"]);

                        if (dtt.Rows[x]["本期收入数量"] == DBNull.Value)
                            dtt.Rows[x]["本期收入数量"] = 0;
                        decimal a2 = Convert.ToDecimal(dtt.Rows[x]["本期收入数量"]);

                        if (dtt.Rows[x]["本期收入单价"] == DBNull.Value)
                            dtt.Rows[x]["本期收入单价"] = 0;
                        decimal b2 = Convert.ToDecimal(dtt.Rows[x]["本期收入单价"]);

                        if (dtt.Rows[x]["本期收入金额"] == DBNull.Value)
                            dtt.Rows[x]["本期收入金额"] = 0;
                        decimal c2 = Convert.ToDecimal(dtt.Rows[x]["本期收入金额"]);

                        if (dtt.Rows[x]["本期发出数量"] == DBNull.Value)
                            dtt.Rows[x]["本期发出数量"] = 0;
                        decimal a3 = Convert.ToDecimal(dtt.Rows[x]["本期发出数量"]);

                        if (dtt.Rows[x]["本期发出单价"] == DBNull.Value)
                            dtt.Rows[x]["本期发出单价"] = 0;
                        decimal b3 = Convert.ToDecimal(dtt.Rows[x]["本期发出单价"]);

                        if (dtt.Rows[x]["本期发出金额"] == DBNull.Value)
                            dtt.Rows[x]["本期发出金额"] = 0;
                        decimal c3 = Convert.ToDecimal(dtt.Rows[x]["本期发出金额"]);

                        dtt.Rows[x]["期初结存数量"] = a - a1;
                        dtt.Rows[x]["期初结存单价"] = b - b1;
                        dtt.Rows[x]["期初结存金额"] = c - c1;

                        dtt.Rows[x]["期末结存数量"] = (a2 - a3) + (a - a1);
                        dtt.Rows[x]["期末结存单价"] = (b2 - b3) + (b - b1);
                        dtt.Rows[x]["期末结存金额"] = (c2 - c3) + (c - c1);

                    }
                }
            }
            
            dataGridView1.DataSource = dtt;
            if (dataGridView1.Rows.Count > 0)
            {

//这里的数据不需要显示,因为期初结存数据=期初收入-期初发出,所以隐藏
                dataGridView1.Columns["期初收入数量"].Visible = false;
                dataGridView1.Columns["期初收入单价"].Visible = false;
                dataGridView1.Columns["期初收入金额"].Visible = false;
                dataGridView1.Columns["期初发出数量"].Visible = false;
                dataGridView1.Columns["期初发出单价"].Visible = false;
                dataGridView1.Columns["期初发出金额"].Visible = false;
            }
            int sum0 = 0;
            decimal sum1 = 0;
            decimal sum2 = 0;
            int sum3 = 0;
            decimal sum4 = 0;
            decimal sum5 = 0;
            int sum6 = 0;
            decimal sum7 = 0;
            decimal sum8 = 0;
            int sum9 = 0;
            decimal sum10 = 0;
            decimal sum11 = 0;
            int aa = 0;
            decimal bb = 0;
            decimal cc = 0;
            int dd = 0;
            decimal ee = 0;
            decimal ff = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                aa += Convert.ToInt32(dataGridView1.Rows[i].Cells["期初收入数量"].Value);
                bb += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初收入单价"].Value);
                cc += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初收入金额"].Value);

                dd += Convert.ToInt32(dataGridView1.Rows[i].Cells["期初发出数量"].Value);
                ee += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初发出单价"].Value);
                ff += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初发出金额"].Value);

                sum0 += Convert.ToInt32(dataGridView1.Rows[i].Cells["期初结存数量"].Value);
                sum1 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初结存单价"].Value);
                sum2 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期初结存金额"].Value);

                sum3 += Convert.ToInt32(dataGridView1.Rows[i].Cells["本期收入数量"].Value);
                sum4 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期收入单价"].Value);
                sum5 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期收入金额"].Value);

                sum6 += Convert.ToInt32(dataGridView1.Rows[i].Cells["本期发出数量"].Value);
                sum7 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期发出单价"].Value);
                sum8 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["本期发出金额"].Value);

                sum9 += Convert.ToInt32(dataGridView1.Rows[i].Cells["期末结存数量"].Value);
                sum10 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期末结存单价"].Value);
                sum11 += Convert.ToDecimal(dataGridView1.Rows[i].Cells["期末结存金额"].Value);
            }
            string aaa = aa.ToString();
            string bbb = bb.ToString();
            string ccc = cc.ToString();
            string ddd = dd.ToString();
            string eee = ee.ToString();
            string fff = ff.ToString();
            string ssum0 = sum0.ToString();
            string ssum1 = sum1.ToString();
            string ssum2 = sum2.ToString();
            string ssum3 = sum3.ToString();
            string ssum4 = sum4.ToString();
            string ssum5 = sum5.ToString();
            string ssum6 = sum6.ToString();
            string ssum7 = sum7.ToString();
            string ssum8 = sum8.ToString();
            string ssum9 = sum9.ToString();
            string ssum10 = sum10.ToString();
            string ssum11 = sum11.ToString();
            string[] row = { "合计", "","","","",aaa, bbb,ccc,ddd,eee,fff,ssum0 , ssum1 , ssum2 , ssum3 , ssum4 , ssum5 , ssum6 , ssum7 , ssum8 , ssum9 , ssum10 , ssum11 };
            ((DataTable)dataGridView1.DataSource).Rows.Add(row);
        }

 

标签:期初,Rows,string,c#,decimal,Convert,winform,统计,dtt
From: https://www.cnblogs.com/45fdsf4s4sdfsddv/p/17638038.html

相关文章

  • 视频云存储/安防监控/视频汇聚EasyCVR平台新增经纬度选取功能
    视频云存储/安防监控EasyCVR视频汇聚平台基于云边端智能协同,支持海量视频的轻量化接入与汇聚、转码与处理、全网智能分发、视频集中存储等。音视频流媒体视频平台EasyCVR拓展性强,视频能力丰富,具体可实现视频监控直播、视频轮播、视频录像、云存储、回放与检索、智能告警、服务器集......
  • 安防监控/视频集中存储/云存储平台EasyCVR增加首页告警类型
    安防监控/视频集中存储/云存储EasyCVR视频汇聚平台,可支持海量视频的轻量化接入与汇聚管理。平台能提供视频存储磁盘阵列、视频监控直播、视频轮播、视频录像、云存储、回放与检索、智能告警、服务器集群、语音对讲、云台控制、电子地图、平台级联、H.265自动转码等功能。由于EasyCV......
  • Visual Components 专业版功能介绍 衡祖仿真
    VisualComponents专业版Professional版本包括VisualComponents精华版Essentials中所有的功能,并提供您用于建模和创建自己的组件的工具。VisualComponents专业版功能1、GEOMETRYSIMPLIFICATION几何体简化通过简化和删除(CAD)模型中不必要的细节,减小文件并提高模拟性能。2、COMP......
  • dotnet7下docker镜像构建之XML打包丢失问题
    注意暂时值适用.NETSDK7.0.100以上其他版本,可以参考举例中的配置host项目省流原因dotnetpublish发布命令对于引用项目的xml不会拷贝到发布目录举例host项目依赖api项目,api项目依赖utils包,utils包中的xml文件在host项目swagger需要使用xml文件一般包引用xml文件输出配......
  • 重塑DTC规则:元气森林的全渠道转型
     元气森林作为迄今为止用5-6年时间最快达到70亿年销售额的饮料品牌(统一、可口可乐、东鹏特饮都花了15年左右,康师傅花了10年)。元气森林于2016年在北京创立,凭借健康产品理念和新潮营销方式,一款主打“0糖0卡0脂”概念的气泡水,迅速赢得年轻消费者青睐,2018-2021营业收入分别增长300%......
  • 视频集中存储/云存储/磁盘阵列EasyCVR平台接入RTSP设备出现离线情况的排查
    安防视频监控/视频集中存储/云存储/磁盘阵列EasyCVR平台可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等,以及支持厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安防视频监控的能力,也具备接入AI智能分析的......
  • 小宾afreeca tv录制浏览器——打造你的个人直播时刻
       你是否是afreecatv的忠实用户或者热爱直播的主播?你是否曾经遇到过想要记录和回放精彩直播内容的需求?不用担心,小宾afreecatv录制浏览器就是为你而生。    小宾afreecatv录制浏览器是一款专为afreecatv平台打造的直播录制软件。它以其强大的功能和简单易用的操......
  • 【ChatGPT系列】彻底搞懂软链接
    [本文出自天外归云的博客园]什么是软链接?软链接(SymbolicLink),也称为符号链接或符号连接,是在文件系统中创建的一种特殊文件类型。软链接提供了一个指向另一个文件或目录的引用,类似于快捷方式或别名。与硬链接不同,软链接是一个指向目标文件或目录的路径的文件,而不是目标文件或目......
  • 25、DHCP动态获取IP地址
    DHCP动态获取IP地址的过程:DHCP通过法师部分广播报文,发现DHCP服务器;DHCP服务器分配一个IP地址通过单播报文;DHCP客户端发送广播报文,请求IP地址;DHCP服务器收到请求报文后,服务器向DHCP客户端发送单播确认报文,包含iIP地址相关信息。DHCP常见的部署方法有interface与global两种模......
  • Docker容器设置自启动
    容器自启动分为两种情况一为新建容器时配置自启参数dockerrun--restart=always容器id或容器名称二为已存在的容器配置自启dockerupdate--restart=always容器id或容器名称取消容器自启dockerupdate--restart=no容器id或容器名称批量设置容器自启dockerupda......