如何解决为什么在将数据表绑定到数据视图之前需要命名数据表?
当我绑定数据视图时,我可以将数据表转储到其中而不会出现任何问题
Dim dtTable As New DataTable
Dim dvTable As New DataView
dvTable.Table = dtTable
这很好用。
但是在我的新项目中,我收到错误“无法绑定到没有名称的数据表”。尝试绑定表时;
Public Class frmInvoiceCategories
Dim intDtInvoiceRowCount As Integer = 0
Dim dtItemCategory1() As DataTable
Dim dtItemCategory2() As DataTable
Dim dvItemCategory2 As New DataView
Dim blnDtCategory2Functional As Boolean = False
Private Sub frmInvoiceCategories_Load(sender As Object,e As EventArgs) Handles MyBase.Load
ReDim cboInvoiceItemTypeMajor(intDtInvoiceRowCount) 'resize the array to fit enough
ReDim cboInvoiceItemTypeMinor(intDtInvoiceRowCount) 'resize the array to fit enough
ReDim dtItemCategory2(intDtInvoiceRowCount) 'resize the array to fit enough dt
subLoadCboItemCategory2() 'load all the item categories for the subcategory cbo
End Sub
Private Sub subLoadCboItemCategory2()
Dim dtItemCategory2Main As New DataTable 'create a temporary dt to check if all the columns are in this dt,then copy the dt
dtItemCategory2Main = fnGetItemCategory2(0) 'get all item subcategories
'check if the dt is functional
If dtItemCategory2Main IsNot nothing Then 'does the dt exist?
If dtItemCategory2Main.Columns.Count > 0 Then 'does the dt have columns?
If dtItemCategory2Main.Columns.Contains("ID") Then 'does it have an ID column?
If dtItemCategory2Main.Columns.Contains("Category2") Then 'does it have a dislpayname column?
blnDtCategory2Functional = True 'table is complete enough to run
Else
MsgBox("Data table category2 doesn't have category2 column")
End If
Else
MsgBox("Data table category2 doesn't have ID column")
End If
Else
MsgBox("Data table category2 doesn't have columns")
End If
Else
MsgBox("Data table category2 is nothing")
End If
If blnDtCategory2Functional = True Then 'if the dt is complete
For intIndex = 0 To intDtInvoiceRowCount Step 1 'count through all the rows in the dt
dtItemCategory2(intIndex) = New DataTable 'create a new instance of the dt
dtItemCategory2(intIndex) = dtItemCategory2Main 'copy the main dt
Next
Else 'the dt is incomplete
MsgBox("form is unusable,closing down")
Me.Close()
End If
End Sub
Private Sub cboInvoiceItemTypeMajor_SelectionChangeCommitted(ByVal sender As Object,ByVal e As System.EventArgs)
dvItemCategory2.Table = dtItemCategory2(intIndex)
End Sub
End Class
为什么我突然要给表格命名?对数组中的表对象的引用就在那里。 不给它一个名字就可以完成吗?
解决方法
正如我一直在说的,DataTable
已经有一个与之关联的 DataView
,所以只需使用它。据我所知,从那段有点狡猾的代码中可以看出,你应该能够改变这一点:
Dim dvItemCategory2 As New DataView
为此:
Dim dvItemCategory2 As DataView
还有这个:
dvItemCategory2.Table = dtItemCategory2(intIndex)
为此:
dvItemCategory2 = dtItemCategory2(intIndex).DefaultView
编辑:
关于问题所指的具体问题,我不知道您是如何做到这一点的,因为我刚刚查看了该 DataView.Table
属性的源代码,它看起来像这样:
[RefreshProperties(RefreshProperties.All)]
[ResDescription("DataViewTableDescr")]
[DefaultValue(null)]
[TypeConverter(typeof (DataTableTypeConverter))]
[ResCategory("DataCategory_Data")]
public DataTable Table
{
get => this.table;
set
{
Bid.Trace("<ds.DataView.set_Table|API> %d#,%d\n",this.ObjectID,value != null ? value.ObjectID : 0);
if (this.fInitInProgress && value != null)
{
this.delayedTable = value;
}
else
{
if (this.locked)
throw ExceptionBuilder.SetTable();
if (this.dataViewManager != null)
throw ExceptionBuilder.CanNotSetTable();
if (value != null && value.TableName.Length == 0)
throw ExceptionBuilder.CanNotBindTable();
if (this.table == value)
return;
this.dvListener.UnregisterMetaDataEvents();
this.table = value;
if (this.table != null)
this.dvListener.RegisterMetaDataEvents(this.table);
this.SetIndex2("",DataViewRowState.CurrentRows,(IFilter) null,false);
if (this.table != null)
this.OnListChanged(new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged,(PropertyDescriptor) new DataTablePropertyDescriptor(this.table)));
this.OnListChanged(DataView.ResetEventArgs);
}
}
}
本次讨论的相关部分是:
if (value != null && value.TableName.Length == 0)
throw ExceptionBuilder.CanNotBindTable();
我不确定为什么使用没有名称的 DataTable
会出现问题 - 将 DataTable
作为参数的构造函数没有这样的限制 - 但很明显你不能。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。