在c#中从数据表输入值到单元格时出错 [英] Error on entering values to cell from datatable in c#

查看:84
本文介绍了在c#中从数据表输入值到单元格时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从datatable输入datagridview中的单元格值。但它给了我一个错误。我在dataGrid的CellEnterEvent中执行此操作。

I am entering value to cell in datagridview from datatable. But it gives me an error. I do it in CellEnterEvent of dataGrid.

if (DataGrid.CurrentCell.ColumnIndex == 6)
                {
                    string getprice = "SELECT " + DataGrid.SelectedRows[0].Cells[3].Value.ToString() + " FROM " +
                        "" + DataGrid.SelectedRows[0].Cells[2].Value.ToString() + "_Mstr ";
                    DataTable dt = globalData.q.select(getprice);
                    DataGrid.CurrentCell.Value = dt.Rows[0][0].ToString();
                }

                if (DataGrid.CurrentCell.ColumnIndex == 7)
                {
                    if (DataGrid.SelectedRows[0].Cells[5].Value.ToString() != "" && DataGrid.SelectedRows[0].Cells[6].Value.ToString() != "")
                        DataGrid.CurrentCell.Value = Convert.ToDouble(DataGrid.SelectedRows[0].Cells[5].Value) * Convert.ToDouble(DataGrid.SelectedRows[0].Cells[6].Value);
                    else
                        DataGrid.CurrentCell.Value = 0;
                }





在其他单元格(列索引= 7)中,它可以正常工作,但在单元格中输入值时来自Datatable的6它给了我一个错误。 (两个值的类型相同。)错误是





In other cell (column Index = 7), it works perfrctly, but when entering the value in cell no 6 from Datatable it gives me an error. (The type of both values are same.) The error is

The following error occurred in datagridview :
System.Exception:0.91is not valid value for Int32.---->
System.FormatException: Input string was not in a correct format.
at System.Number.String.ToNumber(String str, NumberStyle options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at...



(写得太久了。)有什么帮助吗?


(Too long to write.)Any help?

推荐答案





此错误可能是因为值 dt.Rows [0] [0] .ToString()对Int32无效。检查其中的值,并检查DBNull值。如果每件事都有效,那么尝试分配到单元格。



试试这个,

Hi,

this error are getting may be because the value "dt.Rows[0][0].ToString()" is not valid for Int32. check the value in it and also check for DBNull value. if every thing is valid then try to assign to cell.

try this,
int check;
if(int.TryParse(dt.Rows[0][0].ToString(), out check))
{
//its a valid number
DataGrid.CurrentCell.Value = dt.Rows[0][0].ToString();
}
else
DataGrid.CurrentCell.Value = 0;



希望它有所帮助。


hope it helps.


这篇关于在c#中从数据表输入值到单元格时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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