Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data grid view 1

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
CA
How could I show a datagrid view that limits the items shown by an item in a combo box on the same form. In other words I want to select something in a combo box on a form and use that data to select only related data to show in a data grid view.
 
The same way as I told you in one of your previous threads - create a DataView and set it to your DataTable. When your SelectedIndex changes of your ComboBox, alter the filter of your DataView. Since your DataGridView is bound to your DataView, the contents will be automatically updated.
 
I tried this but it is showing all records and highlights the first one it finds with the corresponding record to the pull down box. How can I alter this code so only records that correspond to the pull down box show.

Dim MyView As DataView

Sub SomeRoutine()
If MyView Is Nothing Then MyView = New DataView
MyView.Table = WorkProcessDataSet.Tables("WorkRelease")
MyView.RowFilter = "Job# = "
WorkReleaseDataGridView.DataSource = MyView
End Sub
 
You need to set the RowFilter to something. In the SelectedIndexChanged event of your ComboBox, call some code that sets the RowFilter. Here's some pseudocode:

Code:
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim CurrentRow As DataRowView = CType(Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex), DataRowView)
        Dim CurrentJobNumber As String = CurrentRow.Item("Job#")
        MyView.RowFilter = "Job# = " & CurrentJobNumber.ToString
    End Sub
 
RiverGuy thanks but I get an error with this code that says RowFilter is not a member of Windows.Systems.Forms.DataGridView
How can I correct this. Thanks in advance

Private Sub Job_ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Job_ComboBox.SelectedIndexChanged

Dim CurrentRow As DataRowView = CType(Me.Job_ComboBox.Items(Me.Job_ComboBox.SelectedIndex), DataRowView)
Dim CurrentJobNumber As String = CurrentRow.Item("Job#")
WorkReleaseDataGridView.RowFilter = "Job# = " & CurrentJobNumber.ToString

End Sub
 
The DataGridView does not take a RowFilter. The DataView takes a RowFilter. A DataGridView is a Windows Forms Control. A DataView is an object for filtering a DataTable which can be used as a DataSource for a DataGridView.

 
Im lost. I have a data gridview Im filtering. What do I have to change??? Thanks for hanging in.
 
A DataSet is an object to store data. It contains a collection of DataTables.

A DataTable is an object to store data. It contains a collection of DataColumns and DataRows.

A TableAdapter or DataAdapter helps you to fill these objects with data.

DataSets and DataTables may be used as DataSources for Windows Forms Controls (the widgets you place on the screen).

A DataView is an object that provides a filtered view of the data in a DataTable. A DataView may be used as a data source for Windows Forms Controls.

A DataGridView is a Windows Forms Control. It's a widget you place on your screen. It can use a DataView as a data source.

So you need to create a DataView to be used as the data source for your DataGridView.
 
I saw your example on the other post for a data view and I have some questions. Where is the combobox tied in to filter this view? What is SomeColumn and SomeNumber tied into???
Thanks again.

Dim MyView As DataView

Sub SomeRoutine()
If MyView Is Nothing Then MyView = New DataView
MyView.Table = MyDataSet.Tables("SomeTable")
MyView.RowFilter = "SomeColumn = " & SomeNumber.ToString()
MyDataGridView.DataSource = MyView
End Sub
 
SomeColumn is a generic name for your column you want to filter on. You would replace it with Job# for example. SomeNumber is just a generic value. You would replace this with CurrentJobNumber.ToString which I have in a previous example in this thread.
 
Im now using this code without errors but what it does is highlight one of the items that correspond to the JobNo and keeps all other records showing. How can I change this so only the records show that have corresponding JobNo


Dim WRView As DataView
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If WRView Is Nothing Then WRView = New DataView
WRView.Table = WorkProcessDataSet.Tables("WorkRelease")
Dim CurrentRow As DataRowView = CType(Me.JobCB.Items(Me.JobCB.SelectedIndex), DataRowView)
Dim CurrentJobNumber As String = CurrentRow.Item("JobNo")
WRView.RowFilter = "JobNo = " & CurrentJobNumber.ToString
End Sub
 
I have no idea other than maybe you mistyped something. Add a new test form called Form4, and paste in this code. Run Test4, examine the code, and you should be able to see how it works
Code:
Public Class Form4
    Dim WorkProcessDataSet As New DataSet
    Dim DV As New DataView
    Dim WithEvents DGV As New DataGridView
    Dim WithEvents CB As New ComboBox
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create a DataGridView
        Me.Controls.Add(DGV)
        DGV.Height = 400
        DGV.Width = 400
        DGV.Location = New Point(0, 0)

        'Add a ComboBox
        Me.CB.Location = New Point(0, Me.DGV.Top + Me.DGV.Height + 10)
        Me.Controls.Add(Me.CB)
        Me.CB.DropDownStyle = ComboBoxStyle.DropDownList

        'Add the WorkRelease Table to the DataSet
        Dim dt As New DataTable
        dt.TableName = "WorkRelease"
        dt.Columns.Add("JobNo", System.Type.GetType("System.Int32"))

        'Add a lookup table for the ComboBox to the DataSet
        Dim dt2 As New DataTable
        dt2.TableName = "LookupValues"
        dt2.Columns.Add("JobNo", System.Type.GetType("System.Int32"))

        'Set the table of the DataView
        DV.Table = dt

        'Add some fake rows to the WorkRelease DataTable
        Dim dr As DataRow = dt.NewRow
        dr.Item(0) = 0
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr.Item(0) = 1
        dt.Rows.Add(dr)

        dr = dt.NewRow
        dr.Item(0) = 2
        dt.Rows.Add(dr)

        'Add some fake rows to the LookupValues DataTable
        dr = dt2.NewRow
        dr.Item(0) = 0
        dt2.Rows.Add(dr)

        dr = dt2.NewRow
        dr.Item(0) = 1
        dt2.Rows.Add(dr)

        dr = dt2.NewRow
        dr.Item(0) = 2
        dt2.Rows.Add(dr)

        'Bind the DataGridView to the DataView
        Me.DGV.DataSource = DV

        'Bind the ComboBox to the Lookup table
        Me.CB.DataSource = dt2
        Me.CB.DisplayMember = "JobNo"
        Me.CB.ValueMember = "JobNo"

    End Sub

    Private Sub CB_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CB.SelectedIndexChanged
        Dim drv As DataRowView = Me.CB.Items(Me.CB.SelectedIndex)
        Me.DV.RowFilter = "JobNo = " & drv.Item("JobNo").ToString
    End Sub
End Class
 
Thanks for hanging in River Guy form4 shows a table with one column named JobNo. There is a 0 in the row under it. There is no other data and no combobox created? What else do I need to do?
 
Form size is not a problem. I noticed the cb is tied to an uncreated table. I need it to get its picks from an table that already exists.
 
I know. This is an example for you to use as a guide to apply to your own case.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top