0

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:

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

2
  • 3
    add the sorting in the SQL query using the Order By clause Commented Feb 28, 2022 at 15:06
  • @PaulKaram I tried that one like: SELECT Name, Email, date(Date) as dateOne, Time, Preferred GP/Nurse Name FROM appointmentdetails ORDER BY (Date)DESC; But it didnt work Commented Feb 28, 2022 at 15:12

2 Answers 2

3

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";
Sign up to request clarification or add additional context in comments.

Comments

3

You need to add your sorting to the Query

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 ORDER BY date DESC`";
                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);
            }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.