In sql server , my query brings out more than one row of data


Now i am trying to make it in a dtagrid that the datagrid shows allpossible rows that is supposed to show .This is the code

                Dim objCommand As New SqlCommand("SELECT * FROM Pricelist WHERE [SECURITY] ='" & Me.cboCompany.SelectedItem & "'", objConnection)
                Dim Reader As SqlDataReader = objCommand.ExecuteReader()

                While Reader.Read

                    Me.dtgCompanyPriceList.Rows(0).Cells(0).Value = Reader("COMPANY ID").ToString
                    Me.dtgCompanyPriceList.Rows(0).Cells(1).Value = Reader("SYMBOL").ToString
                    Me.dtgCompanyPriceList.Rows(0).Cells(2).Value = Reader("SECURITY").ToString
                    Me.dtgCompanyPriceList.Rows(0).Cells(3).Value = Reader("OPENING PRICE").ToString
                    Me.dtgCompanyPriceList.Rows(0).Cells(4).Value = Reader("CLOSING PRICE").ToString
                    Me.dtgCompanyPriceList.Rows(0).Cells(5).Value = Reader("PUBDATE").ToString
                    Me.dtgCompanyPriceList.Rows(0).Cells(6).Value = Reader("DESCRIPTION").ToString

                End While


so anyway to make multiple rows to show will be appreciated thanks.

to display data in the datagrid use the below code and then call the method with datagridview
Note- in select query call only the fields that u want to display in your datagridview1

Public Function GetData() As DataView
        'open connection
        Dim SelectQry = "SELECT * FROM Pricelist WHERE [SECURITY] ='" & Me.cboCompany.SelectedItem & "'"
        Dim SampleSource As New DataSet
        Dim TableView As DataView
            Dim SampleCommand As New SqlCommand()
            Dim SampleDataAdapter = New SqlDataAdapter()
            SampleCommand.CommandText = SelectQry
            SampleCommand.Connection = Connection
            SampleDataAdapter.SelectCommand = SampleCommand
            TableView = SampleSource.Tables(0).DefaultView
        Catch ex As Exception
            Debug.Print("Exception: ")
            Throw ex
        End Try
       'close connection
        Return TableView
    End Function

and when calling datagrid write the following code like in button click or form load event...

Datagridview1.DataSource = GetData()

Hope this helps you

Edited 4 Years Ago by poojavb

This question has already been answered. Start a new discussion instead.