Question

VB.net sorting datagridview columns by selecting combobox item. Hey guys im stuck on a problem within...

VB.net sorting datagridview columns by selecting combobox item.

Hey guys im stuck on a problem within my project, I have a datagridview named petdatagridview which is populated with data from an sql table called pet.

I have a combobox on the same form as the datagridview named comboboxsort, next to the combobox is a button that changes the sort mode from ascending to descending when clicked I have bound data to the combobox using the following code:

Dim conn As New MySqlConnection("server=localhost; Port=3306; database= tickedoff; username=root;")
        conn.Open()
        Dim da As New MySqlDataAdapter("select Column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'pet' ", conn)
        Dim table As New DataTable
        da.Fill(table)

         comboBoxSort.DataSource = New BindingSource(table, Nothing)
        comboBoxSort.DisplayMember = "column_name"

So the goal is to be able to select a column name from the combobox (column names are: petID, petname, species, breed, dob, gender, weight, customerID)

and when selected the same column in the datagridview will be highlighted/ selected. The sort button next to the combobox changes the sort direction of that column to descending to ascending. I have tried:

petDataGridView.Columns.Item(DataGridViewSelectedColumnCollection) = comboBoxSort.SelectedItem()

and:

petDataGridView.Sort(petDataGridView.Columns(comboBoxSort.SelectedValue))

plus many more with no luck...

Any ideas or suggestions would be very helpful... thanks..

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Design

Coding:

Imports System.Data.SqlClient
Public Class Form1
    Dim con As SqlConnection
    Dim da As SqlDataAdapter
    Dim ds As DataSet
    Dim i As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        con = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Application.StartupPath + "\pets_db.mdf") ''path of my database
        con.Open()
        da = New SqlDataAdapter("select Column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'pet'", con)
        ds = New DataSet
        da.Fill(ds, "f")
        For i = 0 To ds.Tables("f").Rows.Count - 1
            cbsort.Items.Add(ds.Tables(0).Rows(i)(0))
        Next
        cbsort.SelectedIndex = 0
        BindWithsort(cbsort.Text)
    End Sub
    Sub BindWithsort(ByVal sort As String)
        da = New SqlDataAdapter("Select * from pet order by " + sort, con)
        ds = New DataSet
        da.Fill(ds, "cl")
        dgvsort.DataSource = ds.Tables("cl")
    End Sub

    Private Sub cbsort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbsort.SelectedIndexChanged
        BindWithsort(cbsort.Text)
    End Sub
End Class


output:

if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........

Add a comment
Know the answer?
Add Answer to:
VB.net sorting datagridview columns by selecting combobox item. Hey guys im stuck on a problem within...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT