c# form - having data sorted by date in descending order

hello this code may not be enough to have you help- but am hoping it is-
i have c# windows form w/microsoft access database as backend-
The sql statement below fiills a grid with data-
I want to have the data sorted by date in descending order.
I had help w/program awhile ago-
data field names EnterDateTime-
If this is not enough to help- let me know
thank you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace ProjectId_1305852099
{
    public partial class frmView : Form
    {
        
        //OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\data13.mdb");       
        OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Program.dbPath + "\\data13.mdb");        

        public String gstrServiceNo = "";

        public frmView()
        {
            InitializeComponent();
        }

        private void frmView_Load(object sender, EventArgs e)
        {
            aConnection.Open();
            dgvData.AutoGenerateColumns = false;
            fillGrid();
        }

        private void fillGrid()
        {
            string sql = "";

            if (gstrServiceNo == "")
            {

                sql = "SELECT tblHeader.Header_Id, tblDetail.Detail_Id, tblHeader.[Service Number] As ServiceNumber, tblUsers.Name, tblParts.Part_Number, tblHeader.EnterDateTime FROM ((tblHeader INNER JOIN tblDetail ON tblHeader.Header_Id = tblDetail.Header_Id) INNER JOIN tblParts ON tblDetail.Part_Id = tblParts.Parts_Id) INNER JOIN tblUsers ON tblHeader.User_Id = tblUsers.User_Id;";
            }
            else
            {
                sql = "SELECT tblHeader.Header_Id, tblDetail.Detail_Id, tblHeader.[Service Number] As ServiceNumber, tblUsers.Name, tblParts.Part_Number, tblHeader.EnterDateTime FROM ((tblHeader INNER JOIN tblDetail ON tblHeader.Header_Id = tblDetail.Header_Id) INNER JOIN tblParts ON tblDetail.Part_Id = tblParts.Parts_Id) INNER JOIN tblUsers ON tblHeader.User_Id = tblUsers.User_Id WHERE tblHeader.[Service Number] = " + gstrServiceNo.Replace("'", "''") + ";";
            }
            
            OleDbDataAdapter da = new OleDbDataAdapter(sql, aConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dgvData.DataSource = ds.Tables[0].DefaultView;

            btnDelete.Enabled = false ;
            if (dgvData.Rows.Count > 0 )
            {
                btnDelete.Enabled = true;
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvData.SelectedRows.Count > 0)
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = aConnection;
                cmd.CommandText = "Delete From tblDetail Where Detail_Id=" + Convert.ToInt32(dgvData.SelectedRows[0].Cells[1].Value);

                cmd.ExecuteNonQuery();

                fillGrid();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

        }
    }
}

Open in new window

davetoughAsked:
Who is Participating?
 
vossjckCommented:
add to the end of your sql queries.   where it has SQL =

add "Order by tblHeader.EnterDateTime desc" before the last ;
0
 
brutaldevCommented:
To elaborate on vossjck's answer with an example:
...
if (gstrServiceNo == "")
{
  // Look at the end for the ORDER BY clause
  sql = "SELECT tblHeader.Header_Id, tblDetail.Detail_Id, tblHeader.[Service Number] As ServiceNumber, tblUsers.Name, tblParts.Part_Number, tblHeader.EnterDateTime FROM ((tblHeader INNER JOIN tblDetail ON tblHeader.Header_Id = tblDetail.Header_Id) INNER JOIN tblParts ON tblDetail.Part_Id = tblParts.Parts_Id) INNER JOIN tblUsers ON tblHeader.User_Id = tblUsers.User_Id ORDER BY tblHeader.EnterDateTime DESC;";
}
else
{
  // Look at the end for the ORDER BY clause
  sql = "SELECT tblHeader.Header_Id, tblDetail.Detail_Id, tblHeader.[Service Number] As ServiceNumber, tblUsers.Name, tblParts.Part_Number, tblHeader.EnterDateTime FROM ((tblHeader INNER JOIN tblDetail ON tblHeader.Header_Id = tblDetail.Header_Id) INNER JOIN tblParts ON tblDetail.Part_Id = tblParts.Parts_Id) INNER JOIN tblUsers ON tblHeader.User_Id = tblUsers.User_Id WHERE tblHeader.[Service Number] = " + gstrServiceNo.Replace("'", "''") + " ORDER BY tblHeader.EnterDateTime DESC;";
}
...

Open in new window

0
 
davetoughAuthor Commented:
great - thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.