将数据视图转换为数据表

Converting DataView to DataTable

我有一个带有 搜索用户按钮 的表单,当我 select 模式弹出扩展程序将显示该按钮时,用户可以搜索所有员工,当 select 值,值 selected 应该传递给另一个 gridview 的父表单。

现在,我有一个将数据视图转换为数据表的代码。调试时索引 (s) 的值未传递给 (idx)。

Dim RowIndex As Integer = gvUser.EditIndex
    Dim dt As New DataTable
    dt = TryCast(Session("dbCache_User"), DataView).Table.Clone

    Dim dv As New DataView(dt, "", "USR_IDNTY", DataViewRowState.OriginalRows)
    Dim s As String = TryCast(gvUser.Rows(RowIndex).FindControl("lblUSR_IDNTY"), Label).Text
    Dim idx As Integer = dv.Find(s)

    dv(idx)("USR_ID") = row.Cells(1).Text
    dv(idx)("NAME") = row.Cells(2).Text

    gvUser.DataSource = dv
    gvUser.DataBind()

当我尝试调试时。我尝试搜索索引为 4 的一行; (s) 的值为 4,但传递给 (idx) 时为 -1。

我需要找到值 s。

看来您可能没有阅读相关文档。这是来自 DataTable.Clone 方法的文档:

Clone creates a new DataTable with the same structure as the original DataTable, but does not copy any data (the new DataTable will not contain any DataRows). To copy both the structure and data into a new DataTable, use Copy.

如果 dvdt 的视图并且 dt 不包含任何行,那么当然 Find returns -1。即使没有阅读该文档,您为什么不发现您的 DataTable 是空的?这是您应该检查的第一件事。