Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am entering value to cell in datagridview from datatable. But it gives me an error. I do it in CellEnterEvent of dataGrid.
C#
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;
                }


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

C#
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?
Posted
Updated 17-Feb-13 20:10pm
v3

1 solution

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,
C#
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.
 
Share this answer
 
Comments
Dhaval Radhanpara 18-Feb-13 2:16am    
@KARTHIK: it helps. now i don't get the error. But it also doesn't enter value. btw i have updated the question (actually error.). help me on this. i am stuck on it.
i tried your code with double. but gives the same error.
Karthik Harve 18-Feb-13 2:24am    
What is the string value you get from the DataTable ?
Dhaval Radhanpara 18-Feb-13 2:27am    
"0.91" it varies with the values of the two cells mentioned in the query. the values in these cells changes, the string values changes. on every string value , I GET THIS ERROR.
Karthik Harve 18-Feb-13 2:38am    
try using Trim. dt.Rows[0][0].ToString().Trim();
Dhaval Radhanpara 18-Feb-13 2:48am    
same error. stuck on it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900