Luke Luke - 7 days ago 6
C# Question

Import txt file data into datagridview c#

I am trying to import data into my datagridview. The code I have so far is:

private void loadButton_Click(object sender, EventArgs e)
{
if (File.Exists(DATA_FILE_NAME))
fileIn = File.OpenText(DATA_FILE_NAME);
else
{
MessageBox.Show(DATA_FILE_NAME + " does not exist", "Abort Execution", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
if (listCountTextBox.ReadInt(out index))
for (int i = 1; i <= index; i++)
idList.Add(Int32.Parse(fileIn.ReadLine())); <----- Error here
fileIn.Close();
DisplayIDList(displayDGV);
}


The format of the data in the text file looks like this: "000-0000" and there are 1,240,000 values. The error says: Input string was not in a correct format.
Any help would be much appreciated!

Thank You!

Answer

As the others responds is not posible to convert "000-0000" to int, thats the error.

Would work if you change:

idList.Add(Int32.Parse(fileIn.ReadLine().Replace("-","")));
//idList.Add(Int32.Parse(fileIn.ReadLine())); <----- Error here