Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

c# - Adding rows to second column ONLY - populating data using a for loop

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...