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?
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
CodePudding user response:
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";
CodePudding user response:
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);
}
}