Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can convert bellow procedure to fill datagridview with Dapper command;

//Without Dapper
using (MySqlConnection con = new MySqlConnection(conString))
  using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Bars", con))
    cmd.CommandType = CommandType.Text;
    using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
      using (DataTable dt = new DataTable())
        dataGridView1.DataSource = dt;

What I have tried:

var sql = "select * from customers";
List<tblbars> lstBars = MySqlHelp.Connection.Query<tblbars>(sql).ToList();
dgvBars.DataSource = lstBars;

public class TblBars
    public int BarId { get; set; }
    public int Width { get; set; }
    public int Thickness { get; set; }
    public string Type { get; set; }

But can't sort datagridview...
Updated 16-Aug-22 23:24pm
Karthik_Mahalingam 17-Aug-22 4:44am    

1 solution

Your user can sort a DataGridView, it has automatic sorting enabled as standard - all the user has to do is click on the appropriate column header.

If you want to do it programatically, then you have two options:
1) Use the DataGridView.Sort Method (System.Windows.Forms) | Microsoft Docs[^]
2) Sort it in your SQL statement before you load the DGV: SQL ORDER BY Keyword[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900