从DataSet中选择结果

时间:2010-08-16 13:42:52

标签: vb.net

我有一个绑定到DataSet的DataGridView。 我只想要在DataGridView上显示的选择列而不是全部。 然后我有一个文本框,我想从未显示在DataGridView中的列显示一个值(Version_no列)。这可能吗?

在DataGridView上有一个隐藏列并将此值用于文本框会更好吗?

Dim UpdateThreadStart As New ThreadStart(AddressOf QueryDataBase)
Dim CallDataBindToDataGrid As New MethodInvoker(AddressOf Me.DataBindToDataGrid)
Dim MyDataSet As New DataSet
Dim MyDataAdapter As SqlDataAdapter
Dim MyQueryString As String = ""
Dim MyConnection As New SqlConnection("ConnectionString")

Private Sub BtnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
    Try
        MyQueryString = "SELECT * FROM TABLE;"
        UpdateThread = New Thread(UpdateThreadStart)
        UpdateThread.IsBackground = True
        UpdateThread.Name = "UpdateThread"
        UpdateThread.Start()
        While UpdateThread.IsAlive
            Application.DoEvents()
        End While
    Catch ex As Exception
            MessageBox.Show(ex.Message)
    End Try

End Sub

Public Sub DataBindToDataGrid()
    DGVSearchResults.DataSource = MyDataSet
    DGVSearchResults.DataMember = "MyTable"
    MyDataAdapter = Nothing
    MyDataSet = Nothing

End Sub

' Sub routine used by the background thread to query database.
Public Sub QueryDataBase()
    MyDataSet = New DataSet()
    MyConnection.Open()
    Dim cmd As New SqlCommand(MyQueryString, MyConnection)
    MyDataAdapter = New SqlDataAdapter(cmd)
    MyDataAdapter.Fill(MyDataSet, "MyTable")
    MyConnection.Close()
    ' Make asynchronous function call to Form's thread.
    Me.BeginInvoke(CallDataBindToDataGrid)
End Sub

2 个答案:

答案 0 :(得分:0)

几个选项:当用户使用以下任一方法单击给定DataGridView中的行时,您可以使用隐藏列或查找引用数据集中的值:DataGridView.CurrentRow.index或DataGridView.SelectedCells(0).Value或TryCast(DatadGridView.SelectedRows(0).DataBoundItem,DataSetTableAdapter.DataTableRowType).ColumnName

答案 1 :(得分:0)

您可以将Same DataSource设置为TextBox和DataGridView。

将TextBox的DataMember设置为所需的列。

然后,当您在DataGridView上从一行移动到另一行时,TextBox会显示所需的值。