0

我有一个数据网格视图,我在其中输入时间小时,在其下方是 dataGridview 列每列末尾的页脚标签,用于计算每天的活动总小时数,如 |Mon| 周二| 星期三

|1 | 2 | 3

|2 | 3 | 4

总数 3 5 6

对于正常输入,它工作正常,但如果我在星期一时间(比如)第一行从 1 开始做任何更改,我将其更改为 5,那么只有该行总数即将到来

在这种情况下,总数将变为 5 2 3

那是下一行在总计算中不考虑

请让我知道我错在哪里。

 public void calcdaywisetotal()
    {
        try
        {
            if (dgvTimeReport.RowCount > 1)
            {
                decimal montot = 0, tuetot = 0, wedtot = 0, thutot = 0, fritot = 0, sattot = 0, suntot = 0, tottot = 0;
                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {
                    //decimal MonTotal = decimal.Parse(lblMonTotal.Text);
                    montot = montot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Monday"].Value);
                    lblMonTotal.Text = montot.ToString();
                    //monday
                    if (lblMonTotal.Text != null)
                    {
                        string[] strmontot = new string[2];

                        if (lblMonTotal.Text.Contains(","))
                        {
                            strmontot = lblMonTotal.Text.Split(',');
                        }

                        if (lblMonTotal.Text.Contains("."))
                        {
                            strmontot = lblMonTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strmontot[0]);
                        int min = Convert.ToInt32(strmontot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblMonTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblMonTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblMonTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblMonTotal.Text = hr + ".0" + min;
                            }
                        }
                        montot = Convert.ToDecimal(lblMonTotal.Text);
                    }

                }

                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {
                    tuetot = tuetot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Tuesday"].Value);
                    lblTueTotal.Text = tuetot.ToString();
                    //tuesday
                    if (lblTueTotal.Text != null)
                    {
                        string[] strtuetot = new string[2];

                        if (lblTueTotal.Text.Contains(","))
                        {
                            strtuetot = lblTueTotal.Text.Split(',');
                        }

                        if (lblTueTotal.Text.Contains("."))
                        {
                            strtuetot = lblTueTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strtuetot[0]);
                        int min = Convert.ToInt32(strtuetot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblTueTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblTueTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblTueTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblTueTotal.Text = hr + ".0" + min;
                            }
                        }
                        tuetot = Convert.ToDecimal(lblTueTotal.Text);
                    }
                }

                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {
                    wedtot = wedtot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Wednesday"].Value);
                    lblWedTotal.Text = wedtot.ToString();
                    //Wednesday

                    if (lblWedTotal.Text != null)
                    {
                        string[] strwedtot = new string[2];

                        if (lblWedTotal.Text.Contains(","))
                        {
                            strwedtot = lblWedTotal.Text.Split(',');
                        }

                        if (lblWedTotal.Text.Contains("."))
                        {
                            strwedtot = lblWedTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strwedtot[0]);
                        int min = Convert.ToInt32(strwedtot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblWedTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblWedTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblWedTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblWedTotal.Text = hr + ".0" + min;
                            }
                        }
                        wedtot = Convert.ToDecimal(lblWedTotal.Text);
                    }
                }

                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {
                    thutot = thutot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Thursday"].Value);
                    lblThuTotal.Text = thutot.ToString();
                    //Thursday
                    if (lblThuTotal.Text != null)
                    {
                        string[] strthutot = new string[2];

                        if (lblThuTotal.Text.Contains(","))
                        {
                            strthutot = lblThuTotal.Text.Split(',');
                        }

                        if (lblThuTotal.Text.Contains("."))
                        {
                            strthutot = lblThuTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strthutot[0]);
                        int min = Convert.ToInt32(strthutot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblThuTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblThuTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblThuTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblThuTotal.Text = hr + ".0" + min;
                            }
                        }
                        thutot = Convert.ToDecimal(lblThuTotal.Text);
                    }
                }

                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {

                    fritot = fritot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Friday"].Value);
                    lblFriTotal.Text = fritot.ToString();
                    //Friday
                    if (lblFriTotal.Text != null)
                    {
                        string[] strfritot = new string[2];

                        if (lblFriTotal.Text.Contains(","))
                        {
                            strfritot = lblFriTotal.Text.Split(',');
                        }

                        if (lblFriTotal.Text.Contains("."))
                        {
                            strfritot = lblFriTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strfritot[0]);
                        int min = Convert.ToInt32(strfritot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblFriTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblFriTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblFriTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblFriTotal.Text = hr + ".0" + min;
                            }
                        }
                        fritot = Convert.ToDecimal(lblFriTotal.Text);
                    }
                }
                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {
                    sattot = sattot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Saturday"].Value);
                    lblSatTotal.Text = sattot.ToString();
                    //Saturday
                    if (lblSatTotal.Text != null)
                    {
                        string[] strsattot = new string[2];

                        if (lblSatTotal.Text.Contains(","))
                        {
                            strsattot = lblSatTotal.Text.Split(',');
                        }

                        if (lblSatTotal.Text.Contains("."))
                        {
                            strsattot = lblSatTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strsattot[0]);
                        int min = Convert.ToInt32(strsattot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblSatTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblSatTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblSatTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblSatTotal.Text = hr + ".0" + min;
                            }
                        }
                        sattot = Convert.ToDecimal(lblSatTotal.Text);
                    }

                }

                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {
                    suntot = suntot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Sunday"].Value);
                    lblSunTotal.Text = suntot.ToString();
                    //Sunday

                    if (lblSunTotal.Text != null)
                    {
                        string[] strsuntot = new string[2];

                        if (lblSunTotal.Text.Contains(","))
                        {
                            strsuntot = lblSunTotal.Text.Split(',');
                        }

                        if (lblSunTotal.Text.Contains("."))
                        {
                            strsuntot = lblSunTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strsuntot[0]);
                        int min = Convert.ToInt32(strsuntot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblSunTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblSunTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblSunTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblSunTotal.Text = hr + ".0" + min;
                            }
                        }
                        suntot = Convert.ToDecimal(lblSunTotal.Text);
                    }
                }

                for (int i = 0; i < dgvTimeReport.RowCount; i++)
                {
                    tottot = tottot + Convert.ToDecimal(dgvTimeReport.Rows[i].Cells["Total1"].Value);
                    lblTotTotal.Text = tottot.ToString();
                    //Total
                    if (lblTotTotal.Text != null)
                    {
                        string[] strtottot = new string[2];

                        if (lblTotTotal.Text.Contains(","))
                        {
                            strtottot = lblTotTotal.Text.Split(',');
                        }

                        if (lblTotTotal.Text.Contains("."))
                        {
                            strtottot = lblTotTotal.Text.Split('.');
                        }

                        int hr = Convert.ToInt32(strtottot[0]);
                        int min = Convert.ToInt32(strtottot[1]);

                        if (min < 60)
                        {
                            if (min >= 10)
                            {
                                lblTotTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblTotTotal.Text = hr + ".0" + min;
                            }
                        }
                        else
                        {
                            hr = hr + 1;
                            min = min - 60;
                            if (min >= 10)
                            {
                                lblTotTotal.Text = hr + "." + min;
                            }
                            else
                            {
                                lblTotTotal.Text = hr + ".0" + min;
                            }
                        }
                    }
                    tottot = Convert.ToDecimal(lblTotTotal.Text);
                }
            }

            else
            {
                lblMonTotal.Text = "0.00";
                lblTueTotal.Text = "0.00";
                lblWedTotal.Text = "0.00";
                lblThuTotal.Text = "0.00";
                lblFriTotal.Text = "0.00";
                lblSatTotal.Text = "0.00";
                lblSunTotal.Text = "0.00";
                lblTotTotal.Text = "0.00";
            }
        }
        catch (Exception Excp)
        {
            MessageBox.Show(Excp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            log.Fatal("Exception -" + Excp.Message);
            return;
        }
    }
4

1 回答 1

0

检查 if 部分的部分,当不输入时,“montot”清除他的值,
添加 else:

    if (lblMonTotal.Text.Contains(","))
    {
        strmontot = lblMonTotal.Text.Split(',');
    }
    if (lblMonTotal.Text.Contains("."))
    {
        strmontot = lblMonTotal.Text.Split('.');
    }
    else
    {
        strmontot = new string[2] { montot.ToString(), "0" };
    }


    else if (hr != 0)
    {
        lblMonTotal.Text = hr + ".0" + min;
    }

编辑:他们将“else”更改为“else if”:

于 2013-09-18T20:43:03.173 回答