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.
LVL 1
Gene KlamerusTechnical ArchitectAsked:
Who is Participating?
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:
Hi,

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")

        Try
            If Conn.State <> ConnectionState.Open Then
                Conn.Open()
            End If

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

            DR = Cmd.ExecuteReader()

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

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

    End Function
0
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.
0
Alfred A.Commented:
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Gene KlamerusTechnical ArchitectAuthor Commented:
It looks to me like this is selecting rows already returned to the initial datagridview.
0
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.
0
Alfred A.Commented:
0
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 http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx.
0
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. :-)
0
CodeCruiserCommented:
I would use a simple DataAdapter to load the data when needed. So for example

btnLoad_Click:

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

dbadp.Fill(dTable)
dbadp.Dispose
DataGridView1.DataSource = dTable
0

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.