Click here to Skip to main content
11,930,788 members (55,932 online)
Rate this:
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 20:49pm
Edited 17-Feb-13 21: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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.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