如何让动态总和在文本框的datagridview列 [英] how to get sum dynamically of datagridview column in textbox

查看:138
本文介绍了如何让动态总和在文本框的datagridview列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到的datagridview的列的总和,并显示在文本框...每个条目后的总和应该是动态服用。对于我使用的是文本框的TextChanged事件,但是当该条目使其不显示任何结果。我想动态获取的结果在文本框中。我想避免的总和tghe使用按钮



这里是文本框TextChanged事件的一段代码:

 私人无效textBox9_TextChanged(对象发件人,EventArgs五)
{
双总和= 0;
的for(int i = 0; I< dataGridView2.Rows.Count ++ I)
{
总和+ = Convert.ToDouble(dataGridView2.Rows [I] .Cells [5 ]。值);
}
textBox9.Text = sum.ToString();
}


解决方案

您已经有了你的代码右,而是我已经把它放在CellValueChanged事件和检查,以确保它是你正在寻找相同的细胞。这会得到,如果单元格的值变化解雇,或者如果一个新行被添加,并随后得到赋值。

 私人无效dataGridView2_CellValueChanged(对象发件人,DataGridViewCellEventArgs E)
{
如果(e.ColumnIndex == 5)
textBox9.Text = CellSum()的ToString()。
}

私人双CellSum()
{
双总和= 0;
的for(int i = 0; I< dataGridView2.Rows.Count ++ I)
{
双D = 0;
Double.TryParse(dataGridView2.Rows [I] .Cells [5] .Value.ToString(),出D);
+总和= D;
}
返回总和;
}



我分手了你的逻辑放到一个单独的方法,使柜面你曾经想要得到其他的东西的总和,你可以简单地调用该方法。我想你可以只是读textbox.Text,但是......



我也用Double.TryParse因为它提供柜面值一些基本的保护,鉴于电池实际上不是一个号码。我不知道这个数据的来源是什么(用户从外部进入/?),所以我想我会加一点保护。



修改捐赠提出了一个伟大的一点,我editted解决方案,包括正在修改单元格的值。我用CellValueChanged事件,而不是CellEndEdit,柜面用户以外的东西可以改变它的值。


i want to get the sum of a column of datagridview and show that in textbox...after each entry the sum should be take dynamically. for that i am using textChanged event of a textbox but when the entry is made it does not show any result. i want to get the result dynamically in the textbox. i want to avoid tghe use of button for sum.

here is a piece of code for the textbox textChanged event:

private void textBox9_TextChanged(object sender, EventArgs e)
        {
            double sum = 0;
            for (int i = 0; i < dataGridView2.Rows.Count; ++i)
            {
                sum += Convert.ToDouble(dataGridView2.Rows[i].Cells[5].Value);
            }
            textBox9.Text = sum.ToString();
        }

解决方案

You've got your code right, but instead I've put it in the CellValueChanged event and check to make sure it was the same cell you're looking for. This will get fired if the cell's value changes, or if a new row is added and subsequently gets a value assigned.

    private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 5)
            textBox9.Text = CellSum().ToString();
    }

    private double CellSum()
    {
        double sum = 0;
        for (int i = 0; i < dataGridView2.Rows.Count; ++i)
        {
            double d = 0;
            Double.TryParse(dataGridView2.Rows[i].Cells[5].Value.ToString(), out d);
            sum += d;
        }
        return sum;
    }

I split up your logic into a separate method, so incase you ever want to get the sum for something else, you can simply call that method. I suppose you could just read textbox.Text, but...

I've also used Double.TryParse as it provides some basic protection incase the value in that given cell is not actually a number. I don't know what the source of the data is (user entered / from an outside source?) so I figured I'd add a little protection.

Edit Grant raises a great point, editted my solution to include cell value being modified. I used the CellValueChanged event instead of CellEndEdit, incase something other than the user can change its value.

这篇关于如何让动态总和在文本框的datagridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆