汇总列错误 [英] summing columns error

查看:54
本文介绍了汇总列错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用此代码vs时显示以下错误:指定的转换无效.标题为:InvalidCastExeption未处理

when i use this code vs shows following error : Specified cast is not valid. with title : InvalidCastExeption was unhandled

 private void TradeLog_Load_1(object sender, EventArgs e)
        {
sqlDataAdapter1.Fill(dataSet11);
decimal allwin;
            decimal win = 0;
            decimal self;

            for (int i = 0; i < DgvLog.Rows.Count; i++)
            {
                self = (decimal)DgvLog.Rows[i].Cells["selfPriceDataGridViewTextBoxColumn"].Value;
                win += self;
            }

            allwin = sum - win;
            label7.Text = win.ToString();
        }



什么是不正确的,我认为它必须工作得很好.找不到任何错误



what is incorrect i think it must be work very good. cant find any mistake

推荐答案

Try Convert.ToDecimal(DgvLog.Rows[i].Cells["selfPriceDataGridViewTextBoxColumn"].Value);代替.

Convert.ToDecimal将尝试解析您的字符串-当要转换的项目已经是一个数字时,使用十进制的转换(如您所做的那样)非常有用.
Try Convert.ToDecimal(DgvLog.Rows[i].Cells["selfPriceDataGridViewTextBoxColumn"].Value); instead.

Convert.ToDecimal will try to parse your string - a cast using decimal (as you have done) is useful when the item being cast is already a number.


问题必须在此处:
self =(十进制)DgvLog.Rows [i] .Cells ["selfPriceDataGridViewTextBoxColumn"].Value;
简单地使用DEBUGGER一定会告诉您有关该行及其原因的信息.

错误仅表示您尝试进行的转换无效.在上面的行中,您正在强制将某些单元格值转换(键入强制转换)为十进制,这是不正确的.

如果确定单元格的值是十进制格式,则可以尝试:
Convert.ToDecimal(DgvLog.Rows[i].Cells["selfPriceDataGridViewTextBoxColumn"].Value);
Issue must be here:
self = (decimal)DgvLog.Rows[i].Cells["selfPriceDataGridViewTextBoxColumn"].Value;
A simple use of DEBUGGER must have told you about the line and the reason.

Error simply means that the conversion that you are trying to make is not valid. In above line, you are forcing a conversion (type casting) of some cell value into decimal which is not right.

If you are sure that the value of the cell is of decimal format then you can try:
Convert.ToDecimal(DgvLog.Rows[i].Cells["selfPriceDataGridViewTextBoxColumn"].Value);


这篇关于汇总列错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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