从数据网格视图单元格 C# 中的值更新标签文本

本文关键字:更新 标签 文本 数据网 数据 网格 视图 单元格 | 更新日期: 2023-09-27 18:31:59

>我有一个数据网格视图,我在其中输入时间小时,在它下面是一个页脚标签,位于 dataGridview 列的每一列末尾的页脚标签,用于计算每天的活动的总小时数,例如 |周一|周二|结婚

|1 | 2 | 3

|2 | 3 | 四

托特 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;
        }
    }

从数据网格视图单元格 C# 中的值更新标签文本

检查你的if部分,当不输入时,"montot"清除他的值,
添加其他:

    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":