Vb.net 为什么在将datatable绑定到dataview之前需要对其命名?

Vb.net 为什么在将datatable绑定到dataview之前需要对其命名?,vb.net,data-binding,dataview,Vb.net,Data Binding,Dataview,当我绑定一个Dataview时,我可以毫无问题地将一个DataTable转储到其中 Dim dtTable As New DataTable Dim dvTable As New DataView dvTable.Table = dtTable 这个很好用 然而,在我的新项目中,当试图绑定一个表时,我得到了错误“不能绑定到没有名称的DataTable” Public Class frmInvoiceCategories Dim intDtInvoiceRowCount As Integer =

当我绑定一个Dataview时,我可以毫无问题地将一个DataTable转储到其中

Dim dtTable As New DataTable
Dim dvTable As New DataView
dvTable.Table = dtTable
这个很好用

然而,在我的新项目中,当试图绑定一个表时,我得到了错误“不能绑定到没有名称的DataTable”

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作为新数据视图
为此:

Dim dvItemCategory2作为数据视图
这是:

dvItemCategory2.Table=dtItemCategory2(intIndex)
为此:

dvItemCategory2=dtItemCategory2(intIndex).DefaultView
编辑:

关于问题所指的具体问题,我不知道您是如何做到这一点的,因为我只是查看了
DataView.Table
属性的源代码,它看起来是这样的:

[RefreshProperties(RefreshProperties.All)]
[ResDescription(“DataViewTableDescriptor”)]
[默认值(空)]
[TypeConverter(typeof(DataTableTypeConverter))]
[重新分类(“数据类别_数据”)]
公共数据表
{
get=>this.table;
设置
{
Bid.Trace(“%d#,%d\n”,this.ObjectID,value!=null?value.ObjectID:0);
if(this.fInitInProgress&&value!=null)
{
this.delayedTable=值;
}
其他的
{
如果(此项已锁定)
抛出ExceptionBuilder.SetTable();
如果(this.dataViewManager!=null)
抛出ExceptionBuilder.CanNotSetTable();
if(value!=null&&value.TableName.Length==0)
抛出ExceptionBuilder.CanNotBindTable();
if(this.table==值)
返回;
this.dvListener.UnregisterMetaDataEvents();
此表=值;
如果(this.table!=null)
this.dvListener.RegisterMetaDataEvents(this.table);
this.SetIndex2(“,DataViewRowState.CurrentRows,(IFilter)null,false);
如果(this.table!=null)
this.OnListChanged(新ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged,(PropertyDescriptor)新DataTablePropertyDescriptor(this.table));
此.OnListChanged(DataView.ResetEventArgs);
}
}
}
本次讨论的相关部分如下:

if(value!=null&&value.TableName.Length==0)
抛出ExceptionBuilder.CanNotBindTable();

我不知道为什么使用没有名字的
DataTable
会有问题-将
DataTable
作为参数的构造函数没有这样的限制-但很明显,你不能。你知道每个
DataTable
都会自动与
DataView
关联吗,是否可通过其
DefaultView
属性访问?事实上,当您在WinForms应用程序中绑定
数据表
时,您在UI中看到的数据实际上来自该
数据视图
,这就是您在绑定到
数据网格视图
时能够对数据进行排序的方式。您需要显式创建一个
DataView
的唯一原因是您需要同一个表的多个视图。还有一点让人困惑的是,在将每个表绑定到dataview之前,必须命名每个表,这与此有什么关系。指向对象的链接就在那里;'Table=dtItemCategory2(intIndex)'这应该足以说明我绑定到Dataview的是哪个对象,对吗?而且,创建一个数据视图比需要同一个表的多个视图有更多的用途;例如正确地筛选和排序,因为表不能这样做,或者我遗漏了什么?是的,您遗漏了什么。你错过了我的评论的相关性。这是相关的,因为如果您首先不创建DataView,并且您不必创建DataView,因为DataView已经存在,那么它们就没有问题要解决。只需使用DataTable的DefaultView,您的问题就会变得毫无意义。此外,在极少数情况下,您确实需要创建DataView,只需使用将DataTable作为参数的构造函数即可。我可能错了,但我相当肯定我以前对一个未命名的DataTable做过这样的操作,没有问题。如果每个DataTable都有自己的dataview,为什么我不能过滤DataTable中的记录
dtItemCategory2(intIndex)。rowfilter=“Category_ID=“&intCategory1_ID
给出了使用dataview时不会发生的错误“rowfilter不是datatable的成员”。如果您有耐心,我仍然有兴趣理解为什么我需要为每个表设置一个名称,这对我来说似乎有点多余,因为只有一个对象具有该引用(dtItemCategory2(intIndex)),命名它有什么意义?为什么不将其名称设置为dtItemCategory2(intIndex)?