c# - 带有组合框的 DataGridView 不填充数据
问题描述
我有一个带有 1 个组合框列的 DataGridView,我用数据填充。我之前在 MSDN 论坛上问过,但他们无法帮助我,因为我使用的是SQLITE。
它看到该列未填充数据,我不知道为什么。我的代码是
if ((cmbCol.DataSource = myList()) == null) 返回;
/************************************************/
private List<string[]> myList()
{
string connectionString = dBFunctions.ConnectionStringSQLite;
helper = new dBHelper(connectionString);
if (helper.Load("SELECT Naam FROM Klant ORDER BY Naam", ""))
{
List<string[]> results = new List<string[]>();
DataSet ds = helper.DataSet;
DataTable dt = ds.Tables[0];
dataRow = helper.DataSet.Tables[0].Rows[0];
results =
dt.Select()
.Select(dataRow =>
dataRow.ItemArray
.Select(x => x.ToString())
.ToArray())
.ToList();
return results;
}
return null;
}
解决方案
我确实更改了代码:
if ((cmbCol.DataSource = myList(1)) == null)
return;
cmbCol.Name = "Naam";
cmbCol.DisplayMember = "";
cmbCol.ValueMember = "Naam";
dataGridView1.Columns.Insert(2, cmbCol); // there is no field with the name 'Naam'
}
}
private List<string> myList(int col)
{
string connectionString = dBFunctions.ConnectionStringSQLite;
helper = new dBHelper(connectionString);
if (helper.Load("SELECT * FROM Klant ORDER BY Naam", ""))
{
List<string> results = new List<string>();
DataSet ds = helper.DataSet;
DataTable dt = ds.Tables[0];
dataRow = helper.DataSet.Tables[0].Rows[0];
foreach(DataRow row in dt.Rows)
{
results.Add(row[col].ToString());
}
return results;
}
return null;
}
但是组合框仍然是空的,为什么?数据在数据源中。
推荐阅读
- visual-studio - Visual Studio 2017 的团队资源管理器不记得服务器
- powershell - Search-ADAccount - 由于超时期限已过而返回此操作
- javascript - 有什么方法可以获取背景
- sharepoint - Sharepoint 调查:动态更新查找
- swift - 选择 UIViewController 类的按钮时如何更新 UICollectionViewCell 中标签的颜色
- c++ - 使用静态 const 结构对相关的类常量进行分组 (C++11)
- laravel - FFmpeg 是我唯一可以用来压缩视频文件的东西吗?
- oracle - 使用 Dapper 执行 Oracle 包的功能
- postgresql - 2个不同字段的nullif内的Postgres COALESCE
- r - 计算以前的事件