1

I calculate the amount of rows I want to have in my second column using a for loop based on reading how many records a file has that has been opened. I have researched and tried various solutions but nothing works, yet it seems so simple. Below is my current code where I retrieve the file's length and do a quick sum, entering a for loop where (at the moment) I am only able to populate the first column.

long Count = 1;
FileInfo Fi = new FileInfo(file);
long sum = (Fi.Length / 1024) - Count;

for (int i = 0; i < sum; i++)
{
    DataGridView1.Rows.Add(Count++);
}

I'm not sure how to do it but I know the above code adds to the first column by default - I don't know how to modify it. I know by:

DataGridView1.Rows.Add("a","b");

... The 'b' value is displayed in the second column, but I don't want anything for now in the first where 'a' is.

I have looked at insert a row with one column datagridview c# but it is related to merging columns, again, I don't want this.

DataGridView1.Rows.Add("",Count++);

Works to an extent, but is not the right way to do it. I'm going to be adding data to the first column later on.

1 Answer 1

1

If you want to omit the value for the first column, just add null or DBNull.Value, e.g.:

DataGridView1.Rows.Add(DBNull.Value, Count++);

This way, the first column will be empty while the second columns contains the value of Count.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.