Adding rows to second column ONLY - populating dat

2019-01-29 03:12发布

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条回答
Deceive 欺骗
2楼-- · 2019-01-29 03:33

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.

查看更多
登录 后发表回答