Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGrid
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;
                }
 
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?
Posted 17-Feb-13 20:49pm
Edited 17-Feb-13 21:10pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Dhaval Radhanpara at 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 at 18-Feb-13 2:24am
   
What is the string value you get from the DataTable ?
Dhaval Radhanpara at 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 at 18-Feb-13 2:38am
   
try using Trim. dt.Rows[0][0].ToString().Trim();
Dhaval Radhanpara at 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)

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 Maciej Los 169
4 /\jmot 169
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100