Link to home
Start Free TrialLog in
Avatar of davetough
davetough

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of vossjck
vossjck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davetough
davetough

ASKER

great - thank you