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..
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.........
VB.net sorting datagridview columns by selecting combobox item. Hey guys im stuck on a problem within...
Start Access. Open the downloaded Access file named exploring_acap_grader_a1_Loans.accdb. Import the exploring_acap_grader_a1_Clients.xlsx Excel workbook into a table named Clients. While importing the data, make sure to select the option First Row Contains Column Headings, and select ClientID as the primary key field. After you have completed the import, open the Clients table in Design view. Change the ClientID field size to 6 and remove the @ symbol from the ClientID format property. Change the ZIP field size to 5. Change...