c#error:输入字符串格式不正确 [英] c# error: Input string not in correct format

查看:626
本文介绍了c#error:输入字符串格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid视图是可编辑的。我得到一个单元格的值,然后计算另一个单元格的值。为此,我已经处理了CellEndEdit和CellBeginEdit事件。

  quantity = 0,quantity1 = 0,quantity_wt1 = 0,quantity_wt = 0 ,ekundag = 0; 
private void grdCaret_CellEndEdit(object sender,DataGridViewCellEventArgs e)
{
try
{
string value = grdCaret.Rows [e.RowIndex] .Cells [e.ColumnIndex ] .Value.ToString();
if(e.ColumnIndex == 1)
{
int val = int.Parse(value);
quantity = val;
ekundag = ekundag + quantity;
tbTotDag_cr.Text = ekundag.ToString();
}

if(e.ColumnIndex == 2)
{
float val = float.Parse(value);
total = val;
ekunrakam = ekunrakam + total;
tbTotPrice_cr.Text = ekunrakam.ToString();
}
grdCaret.Columns [3] .ReadOnly = false;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

private void grdCaret_CellBeginEdit(object sender,DataGridViewCellCancelEventArgs e)
{
rate = 0;
quantity1 = quantity;
total1 = total;
rate =(total1 / quantity1);
if(e.ColumnIndex == 3)
{
grdCaret.Rows [e.RowIndex] .Cells [e.ColumnIndex] .Value = rate.ToString();
grdCaret.Rows [e.RowIndex] .Cells [e.ColumnIndex] .ReadOnly = true;
// quantity = 0;
// total = 0;
}
}

在网格中,我列有数量,总数和率。我在这里收到以上错误:

  if(e.ColumnIndex == 1)
{
int val = int.Parse(value);
quantity = val;
ekundag = ekundag + quantity;
tbTotDag_cr.Text = ekundag.ToString();
}

输入数量并点击gridview中的总列。请帮助我解决这个问题。

解决方案

AFAIK的int.Parse()函数可能会导致这种异常。

您是否尝试检查单元格中的值?是不是可能,一些其他的字符在单元格,不仅数字?例如白色空间。


I have a datagrid view which is editable. I'm getting the value of a cell and then calculate the value of another cell. To do this I have handled CellEndEdit and CellBeginEdit events.

quantity = 0, quantity1 = 0, quantity_wt1 = 0, quantity_wt = 0, ekundag = 0;
private void grdCaret_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        string value = grdCaret.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        if (e.ColumnIndex == 1)
        {
            int val = int.Parse(value);
            quantity = val;
            ekundag = ekundag + quantity;
            tbTotDag_cr.Text = ekundag.ToString();
        }

        if (e.ColumnIndex == 2)
        {
            float val = float.Parse(value);
            total = val;
            ekunrakam = ekunrakam + total;
            tbTotPrice_cr.Text = ekunrakam.ToString();
        }
        grdCaret.Columns[3].ReadOnly = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

private void grdCaret_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    rate = 0;
    quantity1 = quantity;
    total1 = total;
    rate = (total1 / quantity1);
    if (e.ColumnIndex == 3)
    {
        grdCaret.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = rate.ToString();
        grdCaret.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;
        //  quantity = 0;
        //   total = 0;
    }
}

In the grid I have columns as quantity, total and rate. I get the above error here:

if (e.ColumnIndex == 1)
{
    int val = int.Parse(value);
    quantity = val;
    ekundag = ekundag + quantity;
    tbTotDag_cr.Text = ekundag.ToString();
}

When I enter quantity and click on the total column in gridview. Please help me fix this

解决方案

AFAIK the int.Parse() function can possibly cause this kind of exceptions.

Have you tried to check the value in the cell? Isn't it possible, that some other characters are in the cell, not only the numbers? White spaces for example.

这篇关于c#error:输入字符串格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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