2010-08-16 154 views
0

我有一個DataGridView綁定到一個DataSet。 我只想選擇顯示在DataGridView上的列而不是全部列。 然後我有一個文本框,我想要顯示DataGridView中未顯示的列的值(Version_no列)。這可能嗎?從DataSet中選擇結果

在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 

回答

0

有幾個選項:您可以使用一個隱藏的列時,或者當用戶使用或者點擊在給定的DataGridView行中所引用的數據集,查找值:DataGridView.CurrentRow.index,或DataGridView.SelectedCells( 0).Value或TryCast(DatadGridView.SelectedRows(0).DataBoundItem,DataSetTableAdapter.DataTableRowType).ColumnName

+0

TextBox.Text = MyDataSet.Tables(0).Rows(DGVSearchResults.CurrentRow.Index)(「Software_Version」)從數據集中返回當前的datagridrow。是否有可能從DataGridView中隱藏此列? (或者選擇\僅顯示特定列?)因爲這些在DataGrid中不是必需的,所以它們顯示在文本框中。 – madlan 2010-08-31 22:14:08

0

可以將Same DataSource設置爲TextBox和DataGridView。

將TextBox的DataMember設置爲所需的列。

然後,當您從DataGridView的一行移動到另一行時,TextBox將顯示所需的值。