You are here:

C#/Input string was not in a correct format.


QUESTION: Hello Murat.
I am trying to get the text: 1 2 3 4 5 from a comboBox that is inside of a datagridView control and save it into a string variable and then, this string save it into a integer list but i get the exception "Input string was not in a correct format."

This is what i have done:

string selectedText = Convert.ToString((dataGridView9.Rows[1].Cells["comboBox1"] as DataGridViewComboBoxCell).FormattedValue.ToString());
var numberList = Array.ConvertAll(selectedText.Split(' '), int.Parse).ToList();

Could you help me, Thanks.

ANSWER: Hi there,

First of all, FormattedValue is already a string. No need to convert it again. This will be enough.

string selectedText = (dataGridView9.Rows[1].Cells["comboBox1"] as DataGridViewComboBoxCell).FormattedValue;

The error probably says the input string is null, so it cannot convert it. Are you sure you have 2 rows in your datagridview? Rows[1] means second row. Indexes mostly start from zero. So make it Row[0] and give a try.

Hope that helps,


---------- FOLLOW-UP ----------

The following code works perfectly. The index Rows[1] is fine because i got another comboBox in Rows[0]

string selectedText = (dataGridView9.Rows[1].Cells["comboBox1"] as DataGridViewComboBoxCell).FormattedValue;

When i run the program i got the exception "Input string was not in a correct format." in this line of code:

var numberList = Array.ConvertAll(selectedText.Split(' '), int.Parse).ToList();

Remember that the text in comboBox Row[1] is: 1 2 3 4 5, perhaps there is another
way to convert this string numbers into integer numbers:

I'll be waiting for your answer, Thanks.

I'll be waiting for your

Hi again,

Then I'm sure you have a space in front or in the end of the text. Try this in its place:

selectedText.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)

If this also does not work, then you will have to recheck carefully with what character they are separated.

Hope that helps,



All Answers

Answers by Expert:

Ask Experts


Murat Mehmet


I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.


I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Was in R & D for 2 years in a popular Turkish technology website:

2011 Computer Engineering graduation in University of Trakia in Turkey.

©2017 All rights reserved.