walkerofskies1 walkerofskies1 - 3 days ago 5
C# Question

Load list into Datagrid View C#

I have a Windows Forms Application I am working on and am using the language C#. I have a Txt file called "UnorderedIDValues.Txt" that I am importing into a DataGrid View. Here is the code I have so far:

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 (index = 1; index <= 1240000; index++)
idList.Add(index);
fileIn.Close();
DisplayIDList(displayDGV);


}


What I am trying to accomplish is: I have a button to load the list into the DataGrid View, which loads just fine, but it's the whole list which isn't what I want. I have an ETextbox that I am wanting to be able to put in a number and only generate that number of values. I feel like I am pretty close, but I just can't wrap my head around writing the correct logic. There are 1,240,000 Values in that text file that are formatted like this: "000-0000"

Any help would be greatly appreciated. Thank You!

Sid Sid
Answer

I don't know what is the purpose of idList but actually you are reading index value from the TextBox and then you are setting it to 1. And then you cycle till index <= 124000

Change your for loop to the following:

for (i = 1; i <= index; i++)
      idList.Add(i);

You can also do it using Linq avoiding the for loop:

idList.addRange(Enumerable.Range(1, index));
Comments