c# - 将datagridview的列保存在List中
问题描述
我有包含两列(id、name)的datagridview。现在我需要将列名的值添加到List<string>
List<string> name = new List<string>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string w = dataGridView1.Rows[i].Cells[1].Value.ToString();
name.Add(w);
}
当我尝试运行该项目时,出现以下错误。
你调用的对象是空的。
如何修复我的代码?
解决方案
我想如果你使用foreach会更好..
像这样的东西:
foreach (DataGridViewRow i in dataGridView1.Rows)
{
if(i.Cells[1].Value != null){
name.Add(i.Cells[1].Value.ToString());
}
}
推荐阅读
- swift - 为什么 Apple Watch 的本地通知会延迟?
- wordpress - WooCommerce - 显示前。增值税/含 增值税标签
- c++ - 使用 uniform_real_distribution 时 clang 性能下降
- javascript - 选择器在本机反应的平面列表中无法正常工作
- python-3.x - ModuleNotFoundError:安装 pyomo 时没有名为“appdirs”的模块
- python - Python TkInter 从外部函数设置条目文本
- c - 从shell脚本读取然后写入c程序
- php - 使用外部图像的 Wordpress 缩略图 url 错误
- android - Firebase 有时会抛出:NullPointerException
- rest - 资源注释域上集合属性的 Grails REST 更新不起作用