【问题标题】:Updating label text from the value from datagrid view cell C#从 datagridview 单元格 C# 的值更新标签文本
【发布时间】:2013-09-18 11:53:25
【问题描述】:

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

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

【问题讨论】:

    标签: c# winforms datagrid label


    【解决方案1】:

    检查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”:

    【讨论】:

    • 我们不需要其他,因为输入小时只需要这两种格式 9.00 或 9,00 都代表 9 小时。还要检查我们有 if (lblMonTotal.Text != null) 条件的值,如果有人将其保留为 0 或空白,则其他部分是否存在 else { lblMonTotal.Text = "0.00";} 这只是格式化错误,但实际的问题是如果用户更新其他一些以前的值,我希望更新总值。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多