在datagrid中插入新行,使用不同的列C#
我正试图在daragridview(C#)中插入一个新行,witch只有来自databindingsource的6行中的3行(它是一个数据集和一个数据表) 这是我试图编写的代码,但它给我带来了一个异常:在datagrid中插入新行,使用不同的列C#,c#,.net,C#,.net,我正试图在daragridview(C#)中插入一个新行,witch只有来自databindingsource的6行中的3行(它是一个数据集和一个数据表) 这是我试图编写的代码,但它给我带来了一个异常: private void cmdNewRow_CustomExecute(object sender, ExecutionEventArgs<ICommandButtonExecutor> e) { Fev018DataSet ds = (Fev018
private void cmdNewRow_CustomExecute(object sender, ExecutionEventArgs<ICommandButtonExecutor> e)
{
Fev018DataSet ds = (Fev018DataSet)(((BindingSource)FahrzeugBarcodeBindingSource.DataSource).DataSource);
int a = ds.Tables[0].Rows.Count;
ds.Tables[0].NewRow();
customGridViewBarcodes.AddNewRow();
你有什么想法吗
例外情况是无法从此函数将dataset类型的obj强制转换为绑定源
当您在这一行获得此异常时
Fev018DataSet ds = (Fev018DataSet)(((BindingSource)FahrzeugBarcodeBindingSource.DataSource).DataSource);
它只是指FahrzeugBarcodeBindingSource后面的对象。DataSource
不是类型BindingSource
,而是类型DataSet
这条线应该是
Fev018DataSet ds = (Fev018DataSet)FahrzeugBarcodeBindingSource.DataSource;
或
或者您在代码的其他地方错误地将
DataSet
分配给了FahrzeugBarcodeBindingSource.DataSource
,而不是BindingSource
。具体在哪一行?是的,您从弱委托列表中的方法public void Invoke(params object[/args)处获得异常。cs:但是我们在发布的代码中没有看到Invoke方法,请把完整的代码放进去。@StefanaB:你的代码对理解这个场景一点帮助都没有。你能检查一下你的代码中有什么类型的绑定源代码吗。异常表示无法强制转换到数据集,因此强制转换时出现问题。
Fev018DataSet ds = (Fev018DataSet)FahrzeugBarcodeBindingSource.DataSource;
DataSet ds = (DataSet)FahrzeugBarcodeBindingSource.DataSource;