Displaying specific data in datagridview on basis of run time query.

I have got a datagridview on my windows form in VB.NET 2005. The data grid's data source is WR130BindingSource. I have dragged a combo box from WCDESC_I in WC931 table.

My problem is I want the datagrid to display only that specific data (only five columns while the table contains 20 columns) from WR130 table when the user selects a WCDESC_I from WC931.

So what happens is when user selects WCDESC_I from combobox, I find the WCID_I of that row and from this WCID_I found from WC931, I need to find all the records in WR130 which contain the same WCID_I.

Further more, I have to also display the data where DONE_CB in WR130 table is not equal to 1.

Also, I have to filter the data to be displayed in the data grid on basis where WO32.ORDERSTATUS = 3 OR 7. Which means first |I have to find all the MoNumbers in WO32 whose OrderStatus is either 3 or 7. Then I have to find all those MoNumbers in WR130 and then display them.

WC931 and WR130 have WCID_I in common.

WO32 and WR130 have MoNumber in common.

So when the user selects, WCDESC_I from the combo box, I need this query to take effect and show the data in data grid:

SELECT dbo.WR010130.MANUFACTUREORDER_I, RTSEQNUM_I, RTSEQDES_I, SCHEDULESTARTDATE_I, DONECB_I

FROM dbo.WR010130JOIN WO010032ON dbo.WR010130.MANUFACTUREORDER_I= dbo.WO010032.MANUFACTUREORDER_I

JOIN dbo.WC010931ON dbo.WR010130.WCID_I= dbo.WC010931.WCID_I

WHERE dbo.WR010130.DONECB_I<> 1AND dbo.WO010032.MANUFACTUREORDERST_I= 3OR dbo.WO010032.MANUFACTUREORDERST_I= 7AND dbo.WC010931.WCDESC_I=WorkCentreComboBox.Text

ORDERBY dbo.WR010130.SCHEDULESTARTDATE_I

WC931.WCID_I is found from the WCDESC_I which user selects from combobox. All the three data tables namely WC931, WO32, WR130 are in my medataset.

So how can I do this at run time?

[4656 byte] By [japt] at [2007-12-23]
# 1

Please help me out here. I am really struggling for doing this bit.

If you have any ideas how it might work then please let me know.

Thanks.

japt at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Public querycon As New SqlConnection(My.Settings.NewTWOConnection)

Public WR130TableAdapter As New SqlDataAdapter("SELECT dbo.WR010130.MANUFACTUREORDER_I, RTSEQNUM_I, RTSEQDES_I, SCHEDULESTARTDATE_I, DONECB_I " _

& "FROM dbo.WR010130 JOIN WO010032 ON dbo.WR010130.MANUFACTUREORDER_I = dbo.WO010032.MANUFACTUREORDER_I " _

& "JOIN dbo.WC010931 ON dbo.WR010130.WCID_I = dbo.WC010931.WCID_I " _

& "WHERE dbo.WR010130.DONECB_I <> 1 AND dbo.WC010931.WCDESC_I = '" & ComboBox.Text & "' AND (dbo.WO010032.MANUFACTUREORDERST_I = 3 OR dbo.WO010032.MANUFACTUREORDERST_I = 7)" _

& "ORDER BY dbo.WR010130.SCHEDULESTARTDATE_I", querycon)

Public cmdquery As New SqlCommandBuilder(WR130TableAdapter)

Public mydataset As DataSet = New DataSet

Then under the OK button_click even you can put the following code

WR010130DataGridView.Show()

Me.mydataset.Tables("WR010130").Clear()

Me.WR130TableAdapter.SelectCommand.CommandText = "SELECT dbo.WR010130.MANUFACTUREORDER_I, RTSEQNUM_I, RTSEQDES_I, SCHEDULESTARTDATE_I, DONECB_I " _

& "FROM dbo.WR010130 JOIN WO010032 ON dbo.WR010130.MANUFACTUREORDER_I = dbo.WO010032.MANUFACTUREORDER_I " _

& "JOIN dbo.WC010931 ON dbo.WR010130.WCID_I = dbo.WC010931.WCID_I " _

& "WHERE dbo.WR010130.DONECB_I <> 1 AND dbo.WC010931.WCDESC_I = '" & WorkCentreComboBox.Text & "' AND (dbo.WO010032.MANUFACTUREORDERST_I = 3 OR dbo.WO010032.MANUFACTUREORDERST_I = 7)" _

& "ORDER BY dbo.WR010130.SCHEDULESTARTDATE_I"

Me.WR130TableAdapter.Fill(Me.mydataset.Tables("WR010130"))

WR010130DataGridView.DataSource = Me.mydataset.Tables("WR010130")

This should work 100%.

GradProd at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...