Need help adding a query to a datagridview

We have a VB application with a datagridview control on it.

It's connected to a SQL database and displaying the data fine, however we now want to have it only return data that satisfies a specified "select ...." statement.

We can't find an example of how to do this.  We need an example.  This is Visual Studio 2008 using .Net 3.5.  I don't know if that matters though.

Can someone send the code or a pointer to where there is an example of doing this?

We don't want to see how to change what is visible (by hiding rows in the dataview grid) or that sets one dataviewgrid on another.  We want to know how to change the data it selects and returns.

We'll take an example in C# if we can translate it.
Gene KlamerusTechnical ArchitectAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:

Why don't you just use a stored procedure to deal with the select statement and use a data reader to read the results of a stored procedure?

You can do it something like this as an example

Public Function GetSelectResult(ByVal Param1 as string, ByVal Param2 as string) As Boolean

        Dim DR As SqlClient.SqlDataReader
        Dim Conn As New SqlClient.SqlConnection(<your connection string>)

        Dim Cmd As New SqlClient.SqlCommand("usp_GetSelectResult")

            If Conn.State <> ConnectionState.Open Then
            End If

            Cmd.CommandType = CommandType.StoredProcedure
            Cmd.Connection = Conn
            Cmd.Parameters.AddWithValue("@Param2", Param2)

            DR = Cmd.ExecuteReader()

            While DR.Read()
                ''Read you data reader here.
            End While

            Return True
        Catch ex As Exception
            _ErrorDescription = "Error retrieving All Details: " & ex.Message & _
                " - " & ex.Source & " - " & ex.StackTrace
            Return False
            If Conn.State = ConnectionState.Open Then
            End If
        End Try

    End Function
Gene KlamerusTechnical ArchitectAuthor Commented:
The issue is that the end-user display choice and what's in place is the gridview.

Personally, I'd rather do it the old-fashioned way (without this data-bound MS creates), but it's what we've been told to use.
Alfred A.Commented:
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Gene KlamerusTechnical ArchitectAuthor Commented:
It looks to me like this is selecting rows already returned to the initial datagridview.
Gene KlamerusTechnical ArchitectAuthor Commented:
or actually just filtering what to show or not.

What I'm looking for is how to modify the select so that only the rows we want to see are returned from the database in the first place.
Alfred A.Commented:
Gene KlamerusTechnical ArchitectAuthor Commented:
well, now we're slipping over to gridviews, which are different.

Also one of these is just filtering data in the grid already created.

I think I've found what I was looking for
Alfred A.Commented:
Oh yeah.  Sorry.  It is DataGridView.  Windows....  I tend to mix those two from time to time.   :-)

That is good if you found what you are looking for.  Goodluck with your projects. :-)
I would use a simple DataAdapter to load the data when needed. So for example


Dim dTable As New DataTable
Dim dbadp as New SQLDataAdapter("Select * From table Where column = somefilter", connectionstring)

DataGridView1.DataSource = dTable

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.