Click here to Skip to main content
11,806,026 members (54,322 online)
Rate this: bad
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 =;
                    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);
                        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 19:49pm
Edited 17-Feb-13 20:10pm

1 solution

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

Solution 1


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();
DataGrid.CurrentCell.Value = 0;
hope it helps.
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 Richard Deeming 225
1 OriginalGriff 215
2 Sergey Alexandrovich Kryukov 170
3 ppolymorphe 125
4 Abhinav S 118
0 OriginalGriff 3,300
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2015
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