【发布时间】:2019-01-07 06:00:51
【问题描述】:
我正在试验和学习 Windows 窗体应用程序,但我无法弄清楚绑定结构。 根据 MSDN 文档:- 您可以为数据源指定以下任何类的实例:
数据集
数据表
数据视图
数据视图管理器
绑定源
所以,我有一个简单的代码来理解这个结构。
我已经用列表框 listBox1 和 DataTable data_table 初始化了一个简单的 windows 窗体。
static DataTable data_table = new DataTable("ParentTable");
DataColumn column;
DataRow row;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = false;
column.Unique = true;
// Add the Column to the DataColumnCollection.
data_table.Columns.Add(column);
for (int i = 0; i <= 2; i++)
{
row = data_table.NewRow();
row["id"] = i;
data_table.Rows.Add(row);
}
可以直接绑定到DataSource by。
listBox1.DataSource=new BindingSource(data_table , null);
listBox1.ValueMemeber="id";
但是当我使用以下内容时
Binding myBinding = new Binding("DataSource", data_table, "id");
form_m.listBox1.DataBindings.Add(myBinding);
我得到异常
System.ArgumentException:复杂 DataBinding 接受作为数据源 IList 或 IListSource
如果有人能帮我解决这个问题,我将不胜感激。
【问题讨论】:
-
请注意,tagging help page 明确指出:“避免在以下任何格式的标题中插入标签:” ... “[标签]:[问题标题]"
-
如果它真的是 Winforms,
BindingSource可能是您想要使用的 - 或者根本不需要,因为DataTable可以工作
标签: c#