Link to home
Start Free TrialLog in
Avatar of AlphaLolz
AlphaLolzFlag for United States of America

asked on

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.
Avatar of Alfred A.
Alfred A.
Flag of Australia image

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
Avatar of AlphaLolz

ASKER

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.
It looks to me like this is selecting rows already returned to the initial datagridview.
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.
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.
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. :-)
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial