我正在尝试在两个datagridviews之间形成关系父子,但我收到此错误:'table'参数不能为null。参数名称:table“。 [英] I'm trying to form a relationship parent-child between two datagridviews, but I'm getting this error:'table' argument cannot be null.Parameter name: table".
问题描述
我得到了这一行的错误:tblDataSet.Tables.Add(Table_c); I get that error to this line: tblDataSet.Tables.Add(Table_c); 这篇关于我正在尝试在两个datagridviews之间形成关系父子,但我收到此错误:'table'参数不能为null。参数名称:table“。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
有谁知道如何解决它?在此先感谢。
这是代码:
< pre lang =c#> private DataTable Table_c; // Table_c从本地数据库导入
private DataTable Table_a; // Table_a从本地数据库导入
private DataSet tblDataSet;
private void Form1_Load(对象 sender,EventArgs e)
{
this .table_aTableAdapter1.Fill( this 跨度> .DBDataSet1.Table_a);
this .table_cTableAdapter.Fill( this .DBDataSet1.Table_c);
tblDataSet = new DataSet();
tblDataSet.Tables.Add(Table_c);
tblDataSet.Tables.Add(Table_a);
tblDataSet.Relations.Add( Relation1,
Table_c.Columns [ Number],Table_a.Columns [ Number]);
BindingSource bsC = new BindingSource();
bsC.DataSource = tblDataSet;
bsC.DataMember = Table_c;
BindingSource bsA = new BindingSource();
bsA.DataSource = bsC;
bsA.DataMember = Relation1;
table_cDataGridView.DataSource = bsC;
table_aDataGridView.DataSource = bsA;
} 您使用的变量似乎存在一些问题。有两个全局表变量Table_c和Table_a,但是当你通过适配器填充时,你正在使用数据集中的表,当你在这里将表添加到另一个数据集时,你正在使用那些全局变量(没有任何值/对象,未分配)。
如果我在本地使用它们,我会收到此错误
列参数不能为null。参数名称:column
,
到此行:
tblDataSet.Relations.Add(Relation1,Table_c.Columns [数字],Table_a.Columns [数字]);
如果我没有声明它们,我使用 this.DBDataSet1.Table_c
相反,我得到DataTable已经属于另一个DataSet
Does anyone know how can I solve it? Thanks in advance.
This is the code:
private DataTable Table_c; // Table_c is imported from a local database
private DataTable Table_a; // Table_a is imported from a local database
private DataSet tblDataSet;
private void Form1_Load(object sender, EventArgs e)
{
this.table_aTableAdapter1.Fill(this.DBDataSet1.Table_a);
this.table_cTableAdapter.Fill(this.DBDataSet1.Table_c);
tblDataSet = new DataSet();
tblDataSet.Tables.Add(Table_c);
tblDataSet.Tables.Add(Table_a);
tblDataSet.Relations.Add("Relation1",
Table_c.Columns["Number"], Table_a.Columns["Number"]);
BindingSource bsC = new BindingSource();
bsC.DataSource = tblDataSet;
bsC.DataMember = "Table_c";
BindingSource bsA = new BindingSource();
bsA.DataSource = bsC;
bsA.DataMember = "Relation1";
table_cDataGridView.DataSource = bsC;
table_aDataGridView.DataSource = bsA;
}
It seems there some problem with variables you are using. There are two global table varaibles Table_c and Table_a, but you when you are filling through adapter, you are using tables from dataset, and when you are adding tables to another dataset here you are using that global varaibles (which do not have any value/object, are unassigned).
If I use them local, I get this error
"column argument cannot be null.Parameter name: column
",
to this line:
tblDataSet.Relations.Add(Relation1, Table_c.Columns["Number"], Table_a.Columns["Number"]);
If I don't declare them, and I use this.DBDataSet1.Table_c
instead, I get "DataTable already belongs to another DataSet"