Home > Mobile >  Date format in richTextBox, when passing date values from dataGridView
Date format in richTextBox, when passing date values from dataGridView

Time:10-12

A simple SQL query is performed and the dataGridView1 on my form does show the values from the date and name columns of my database, as expected . The dates in the dataGridView1 appear in the desired format ("MM/dd/yyyy").

Next, the contents of the dataGridView1 are passed on to richTextBox1. The date values here (in the richTextBox1) do appear in the desired format ("MM/dd/yyyy"), however they also include a time value of 12:00:00 AM in all cases (eg 2/6/2020 12:00:00 AM).

I want to get rid of this time component of the date values in the richTextBox1. I understand I must declare somehow the desired format for the values of the column date (first column of dataGridView1) in the richTextBox1, but have not managed to do it, so far. Any suggestions?

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = SelectData();

            richTextBox1.Text = "REPORT \n\n";

            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i  )
            {
                richTextBox1.Text = richTextBox1.Text   "-";

                for (int j = 0; j < dataGridView1.Columns.Count; j  )
                {
                    richTextBox1.Text = richTextBox1.Text   "\t"   dataGridView1.Rows[i].Cells[j].Value.ToString()   "\t";
                }
                richTextBox1.Text = richTextBox1.Text   "\n";
            }
        }
        
        private DataTable SelectData()
        {
            DataTable dtSelectData = new DataTable();

            using (MySqlConnection con = new MySqlConnection(connString))
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT date, name FROM person", con))
                {
                    try
                    {
                        con.Open();
                        MySqlDataReader reader = cmd.ExecuteReader();
                        dtSelectData.Load(reader);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Database Message:\n"   ex.Message);
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
            return dtSelectData;
        }

CodePudding user response:

Replace

dataGridView1.Rows[i].Cells[j].Value.ToString() 

with

dataGridView1.Rows[i].Cells[j].Value.ToString("MM/dd/yyyy")

to prevent time.

CodePudding user response:

To format date from dataGridView1 cell, you can cast cell's value (object) to DateTime type and use ToString("Some DateTime Format") on it after.

Safe and simple way to cast is to combine check, that cell value is of DateTime type and cast it to DateTime type if it is. It could be done with is operator:

if (dataGridView1.Rows[i].Cells[j].Value is DateTime dt) { ... }

It calls Pattern matching, you can read about it on enter image description here

  • Related