tags:

views:

49

answers:

2

I have a DataGridView bound to a DataSet. I only want a selection of columns displayed on the DataGridView rather than all. I then have a text box which I want to display a value (Version_no column) from the column not showing in the DataGridView. Is this possible?

Would it be better to have a hidden column on the DataGridView and use this value for the text box?

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
A: 

Several options: You can use a hidden column, or lookup the value in the referenced dataset, when the user clicks a row in the given DataGridView using either: DataGridView.CurrentRow.index, or DataGridView.SelectedCells(0).Value, or TryCast(DatadGridView.SelectedRows(0).DataBoundItem, DataSetTableAdapter.DataTableRowType).ColumnName

Nathan
TextBox.Text = MyDataSet.Tables(0).Rows(DGVSearchResults.CurrentRow.Index)("Software_Version") returns the current datagridrow from the dataset. Is it possible to hide this column from the DataGridView? (Or select\display only specific columns?) As these are not required in the DataGrid now they are displayed in the textbox.
madlan
A: 

You can set the Same DataSource to your TextBox and your DataGridView.

Set DataMember for the TextBox to desired column.

Then when you move from one row to other on DataGridView, TextBox shows the desired value.

x77