仅将行添加到第二列 - 使用 for 循环填充数据 [英] Adding rows to second column ONLY - populating data using a for loop

查看:21
本文介绍了仅将行添加到第二列 - 使用 for 循环填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 for 循环基于读取文件已打开的记录数来计算我想要在我的第二列中包含的行数.我研究并尝试了各种解决方案,但没有任何效果,但似乎很简单.下面是我当前的代码,我在其中检索文件的长度并进行快速求和,然后进入 for 循环,其中(目前)我只能填充第一列.

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");

... 'b' 值显示在第二列中,但我现在不想要任何东西在 'a' 所在的第一列中.

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

我看过插入一行一列datagridviewc# 但它与合并列有关,同样,我不想要这个.

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.

推荐答案

如果要省略第一列的值,只需添加 nullDBNull.Value,例如:

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++);

这样,第一列将为空,而第二列包含Count的值.

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

这篇关于仅将行添加到第二列 - 使用 for 循环填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆