C# winform remove time from datatime

Booney 166 Reputation points
2021-02-28T16:26:01.747+00:00

I am importing an excel file into SqLite database, the results are 2/28/2021 0:00.
I only want the date. When I Manually enter the Datetime picker it is correct.

72628-screenshot-2021-02-28-101908.png

private void btn_Import_Click(object sender, EventArgs e)  
        {  
            OleDbConnection theConnection = new OleDbConnection(@"provider=Microsoft.JET.OLEDB.4.0;data source='" + txtFileName.Text + "';Extended Properties=\"Excel 8.0;HRD=NO;IMEX=1\"");  
  
            theConnection.Open();  
            OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("Select * from[Sheet1$]", theConnection);  
            DataSet theSD = new DataSet();  
            DataTable dt = new DataTable();  
            theDataAdapter.Fill(dt);  
            this.dataGridView1.DataSource = dt.DefaultView;  
        }  
  
        void fillGrid()  
        {  
            con.Open();  
            SQLiteDataAdapter da = new SQLiteDataAdapter("Select * from Information order by ID", con);  
            DataTable dt = new DataTable();  
            da.Fill(dt);  
            dataGridView1.DataSource = dt;  
              
         //   dataGridView1.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy";  
            con.Close();  
        }  
  
        private void btn_Save_Click(object sender, EventArgs e)  
        {  
  
            con.Open();  
            for (int i = 0; i < dataGridView1.Rows.Count; i++)  
            {  
                  
                //if (LibID.Text == "*")  
                //{  
                SQLiteCommand cmd = new SQLiteCommand("insert into Information(Production_Date,Part_Number,Cart,Qty,Location,User)" +  
                    " values('" + dataGridView1.Rows[i].Cells[1].Value + "','" + dataGridView1.Rows[i].Cells[2].Value + "'," +  
                    "'" + dataGridView1.Rows[i].Cells[3].Value + "','" + dataGridView1.Rows[i].Cells[4].Value + "'," +  
                    "'" + dataGridView1.Rows[i].Cells[5].Value + "','" + dataGridView1.Rows[i].Cells[6].Value + "')", con);  
                  
                cmd.ExecuteNonQuery();  
            }  
            con.Close();  
                       MessageBox.Show("Successful Saved");  
                       fillGrid();  
                       txtFileName.Text = string.Empty; // Clear TextBox  
  
  
        }  
            }  
        }  


  
Developer technologies Windows Forms
SQL Server Other
Developer technologies C#
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-02-28T17:37:27.817+00:00

    Try one of solutions:

    private void btn_Import_Click(object sender, EventArgs e)
    {
       . . .
       this.dataGridView1.DataSource = dt.DefaultView;
       dataGridView1.Columns["Production_Date"].DefaultCellStyle.Format = "d";
    }
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Abdulhakim M. Elrhumi 356 Reputation points
    2021-02-28T20:29:39.54+00:00

    Hi

    private void button1_Click(object sender, EventArgs e)
        {
          DateTime dt;
    
          string dt1;
           dt = DateTime.Now;
          string tdDay,tdMonth,tdYear;
          //Befor display date and time
          MessageBox.Show(dt.ToString());
    
          tdDay = (dt.Day).ToString();
          tdMonth = (dt.Month).ToString();
          tdYear = (dt.Year).ToString();
          dt1 = tdDay + "/" + tdMonth + "/" + tdYear;
          //After display date only
           MessageBox.Show(dt1);
    
        }
    

  2. cheong00 3,486 Reputation points Volunteer Moderator
    2021-03-02T07:45:00.547+00:00

    If you don't ever need the time part, modifying your SQLLite select statement to use "substr(Production_Date,0,10) as Production_Date" will do.

    It will be better if you can eliminate the time part when you read from Excel file, though.

    Also note that since for SQLite, the n of varchar(n) is ignored and whatever you stored is never truncated based on it, the other possible strategy to declare the field varchar(10) and hope it will silently truncate the excess characters will fail.

    0 comments No comments

  3. Booney 166 Reputation points
    2021-03-04T22:36:41.29+00:00

    This is what worked.

     OleDbDataAdapter theDataAdapter =
               new OleDbDataAdapter("SELECT [ID], FORMAT([Production_Date], 'M/d/yyyy') as [Production_Date],[Part_Number],[Cart],[Qty],[Process],[Model],[User] FROM [Sheet1$]", theConnection);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.