2 Replies - 2169 Views - Last Post: 08 November 2012 - 11:47 AM Rate Topic: -----

#1 pratik65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 10-September 12

i dont want to get duplicate values from datagridview in my combobox

Posted 08 November 2012 - 11:40 AM

i have a database table and it has some column with duplicate values , and i want to get that column value of the table in a combobox, but i want every value single time not repeatedly ... i tried but it is still giving me all values in the combobox:

        Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=grade;Integrated Security=True;Pooling=False")
        Dim cmd As SqlCommand
        Dim Classes As New List(Of String)
        Try
            conn.Open()
            Dim query As String = "SELECT Class FROM Class"
            cmd = New SqlCommand(query, conn)
            Dim rd As SqlDataReader = cmd.ExecuteReader(Commandbehavior.CloseConnection)
            While rd.Read()
                Classes.Add(rd("Class").ToString)
            End While
            rd.Close()
            conn.Close()

            Me.ComboClass.Items.Clear()
            Me.ComboClass.Items.AddRange(Classes.ToArray)

        Catch ex As Exception
            MessageBox.Show(ex.ToString())


        End Try



Is This A Good Question/Topic? 0
  • +

Replies To: i dont want to get duplicate values from datagridview in my combobox

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: i dont want to get duplicate values from datagridview in my combobox

Posted 08 November 2012 - 11:46 AM

Why not just go straight from the database to the combobox using a SELECT DISTINCT Query?
Was This Post Helpful? 1
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: i dont want to get duplicate values from datagridview in my combobox

Posted 08 November 2012 - 11:47 AM

Look at the distinct method. You are already saving values from rd into Classes list. Before you add them to ComboClass.Items, use suggested distinct method.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1