Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort data by date, time, and name from Mysql database in c#

I have been really struggling to sort this data by date and time. As you can see, I have got this data. In addition, the data table is not taking the same space as the dataGridView space. How do I resolve that problem?

enter image description here

I am trying to sort out this data in c#. I have generated the above table by writing the below code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void ViewAppointment_Load(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection connection = new MySqlConnection("server=localhost;port=****;username=root;password=****;database=appointment; pooling = false; convert zero datetime = True");
                string Query = "SELECT * FROM `appointmentdetails`";
                MySqlCommand myCommand = new MySqlCommand(Query, connection);

                MySqlDataAdapter myAdapater = new MySqlDataAdapter();
                myAdapater.SelectCommand = myCommand;
                DataTable dTable = new DataTable();
                myAdapater.Fill(dTable);
                dataGridView1.DataSource = dTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Thanks for your time

>Solution :

You can either sort directly on the DataGrid:

dataGridView1.Sort(dataGridView1.Columns["Date"], ListSortDirection.Ascending);

Or get the data sorted from sql:

string Query = "SELECT * FROM `appointmentdetails` order by Date";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading