如何在不使用表适配器的情况下填充表单

时间:2011-01-14 20:27:17

标签: vb.net

我可以使用表适配器轻松完成此操作,但我似乎无法在表适配器连接字符串中使用变量,或者在“填充”之前指定要使用的连接字符串。有没有办法在不使用任何绑定的情况下填充表单?

目前我已经使用过这种方法 - 一个填充列表框的搜索表单,在双击操作中我有这个:

Private Sub lstResults_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles lstResults.CellMouseDoubleClick
    'Fills Main Form
    Dim firstcellvalue As String = lstResults.Rows(e.RowIndex).Cells(0).Value.ToString()
    frm_IMT.intPendingID = CInt(lstResults.Rows(e.RowIndex).Cells(0).Value.ToString())
    frm_IMT.Show()

然后当表单加载时:

Me.PendingTableAdapter.Fill(Me.Phone_memo_backendDataSet.Tables(“Pending”),intPendingID)

1 个答案:

答案 0 :(得分:2)

您是否可以更确切地了解您想要实现的目标?当你说你似乎无法使用变量构建你的连接字符串时,你的意思是,为了连接到后端并检索数据?

由于我不确定你想做什么,我会做出一些猜测。它看起来就像你试图在一个表单上填充某种列表(让我们说一个dataGridView)。

然后,您允许用户通过双击行中的任何单元格来选择dataGridview中的项目,这将为包含待处理电话备忘录的表格中的记录检索主键ID。

然后使用此ID向TableAdapter填充表格中的所有记录"待定"它具有PendingID作为主键(从而返回单个记录)或外键(因此返回0到多个记录)。

由于我们不知道您的结果应该在哪里结束(表适配器是否包含单个记录,或者可能包含多个记录?),因此很难回答您的问题。

就个人而言,我会考虑编写一个函数,该函数返回一个DataTable,其中包含您希望填充表单的信息。在此示例中,该函数返回一个dataTable,其中包含每个状态的记录以及相关数据。假设此函数和下一个函数是名为" MyDataFactory"的成员:

'Shared Keyword makes this function Available without instantiating the containing class:
Public Shared Function StatesDataTable(ByVal StateID As Integer) As DataTable

    'Set up your SQL Statement to execute against your back end:
    Dim SQL As String = _
    "SELECT StateID, State, StateName, HUDStateID " & _
    "FROM tblState " & _
    "ORDER BY State"

    'Initialize a DataTable:
    Dim dt As New DataTable

    'The Using Keyword is elegant in that it handles bothersome Disposal tasks
    'for unmanaged objects such as Database Connections:
    Using cn As New OleDb.OleDbConnection(My.Settings.CreateTHisConnection)
        Using cmd As New OleDb.OleDbCommand(SQL, cn)

            'Open the Connection:
            cn.Open()

            'Retreive the Data into a DataReader:
            Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader

            'Load the contents of the Reader into your datatable:
            dt.Load(dr)

            'Clean Up:
            dr.Close()
            cn.Close()

        End Using ' cmd
    End Using ' cn

    'Return the DataTable to calling code:
    Return dt

End Function ' StatesDataTable

如果我使用这个数据表来填充我的主窗体上的列表,其中StateID(例如隐藏为ListBox的Value成员)和StateName(显示,作为所述ListBox的DisplayMember)。当用户单击列表框中的特定状态时,可以从Valuemember属性中检索StateID,并将其作为参数传递给另一个函数,这可能会返回与该状态相关的CountTable数据:

'Shared Keyword makes this function Available without instantiating the containing class:
Public Shared Function CountiesDataTable(ByVal CountyID As Integer) As DataTable

    'Set up your SQL Statement to execute against your back end:
    Dim SQL As String = _
    "SELECT CountyID, StateID, CountyName " & _
    "FROM tblCounties " & _
    "WHERE CountyID = @CountyID " & _
    "ORDER BY CountyName"

    'Initialize a DataTable:
    Dim dt As New DataTable

    'The Using Keyword is elegant in that it handles bothersome Disposal tasks
    'for unmanaged objects such as Database Connections:
    Using cn As New OleDb.OleDbConnection(My.Settings.CreateTHisConnection)
        Using cmd As New OleDb.OleDbCommand(SQL, cn)

            'Add a parameter which limits the result set to counties in the specified state:
            cmd.Parameters.AddWithValue("@CountyID", CountyID)
            'Open the Connection:
            cn.Open()

            'Retreive the Data into a DataReader:
            Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader

            'Load the contents of the Reader into your datatable:
            dt.Load(dr)

            'Clean Up:
            dr.Close()
            cn.Close()

        End Using ' cmd
    End Using ' cn

    'Return the DataTable to calling code:
    Return dt

End Function ' CountiesDataTable

然后可以使用此函数的结果输出加载到弹出窗体或其他一些机制中,以显示与所选状态关联的县。

不确定这些是否是你所追求的,但如果没有更多的继续,我尽我所能。 。

我的方法论在ADO.NET领域有点不正统,但我喜欢它,因为它允许我隐含控制我从后端拉出的东西。对于许多应用程序而言,并非全球可用"共享"这样的功能会派上用场 - 为了按照需要检索国家(或县)的数据表,我只需执行以下操作:

    Dim dtStates As DataTable = MyDataFactory.StatesDataTable
    Dim dtCounties As DataTable = MyDataFactory.Counties(ByVal StateID As Integer)

无论如何,hoipe有帮助。如果你可以澄清你想要做什么,或者找到这种行为,并想要更多信息,请随时跟进。