1

I need help on my DataGridView column date format. I want the date to be "MM/dd/yyyy". I dont need the time.

How can this be done programmatically?

enter image description here

i have tried this code but it doesn't work gridView.Columns[1].DefaultCellStyle.Format = "MM/dd/yyyy";

`var connSettings = ConfigurationManager.ConnectionStrings["MyDB"]; { string CN = connSettings.ConnectionString;

            MySqlConnection conn = new MySqlConnection(CN);

            MySqlCommand cmd = new MySqlCommand("select id, entry_datetime, CONCAT(last_name, ' ' ,first_name, ' ' ,alias) as Name/Code from members order by entry_datetime desc;", conn);

            MySqlDataAdapter data = new MySqlDataAdapter(cmd);
            conn.Open();

            DataTable dt = new DataTable();
            data.Fill(dt);
            gridView.DataSource = dt;


        }


        gridView.Columns[0].Width = 120;
        gridView.Columns[0].HeaderText = "ID";
        gridView.Columns[1].Width = 120;
        gridView.Columns["entry_datetime"].DefaultCellStyle.Format = "MM/dd/yyyy";
        gridView.Columns[1].HeaderText = "Date Received";
        gridView.Columns[2].HeaderText = "Name/Code";`
Kevin Rodriguez
  • 117
  • 1
  • 2
  • 10
  • i am using C# win forms – Kevin Rodriguez Mar 25 '15 at 03:20
  • I tried your code, and it works just fine for me. Are you sure you are referencing the correct column? Columns are 0-based, so an index of 1 refers to the second column. – Reticulated Spline Mar 25 '15 at 03:35
  • I'm not sure how you are even getting the data without using DataBind() method. Anyways, can you please paste the designer code also? – Anup Sharma Mar 25 '15 at 04:25
  • I think it can happen that if `dgv.AutoGenerateColumns` is true then you will lose all your style and formatting properties when the DataSource is reassigned. So in debug mode, check the `DefaultCellStyle.Format` is still set. – Loathing Mar 25 '15 at 07:12

7 Answers7

0

The easiest way is to use DateTime.ToShortDateString() or DateTime.ToLongDateString()

If neither of those formats works take a look at DateTime.ToString(string)

Matt
  • 724
  • 4
  • 11
0

After you set the DataSource of the DataGridView, set the desired format of the column:

dataGridView1.Columns["dateReceived"].DefaultCellStyle.Format = "MM/dd/yyyy";
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • i have tried this `code`gridPatient.Columns["entry_datetime"].DefaultCellStyle.Format = "MM/dd/yyyy";`code` still the "12:00" time is showing up, what could be the problem? – Kevin Rodriguez Mar 25 '15 at 03:41
0

If you're trying to format a databound column, you should use DefaultCellStyle.Format. Be sure the value of Format attribute isn't being overwritten on Designer.

The code below works fine:

        Random rnd = new Random();

        dataGridView1.Columns.Clear();
        dataGridView1.Columns.Add("colDate", "Random Date");
        dataGridView1.Columns["colDate"].DefaultCellStyle.Format = "MM/dd/yyyy";
        dataGridView1.Columns["colDate"].DataPropertyName = "Date";

        DataTable dt = new DataTable();
        dt.Columns.Add("Date", typeof(DateTime));
        for (int i = 1; i <= 10; i++)
        {
            DataRow row = dt.NewRow();
            row["Date"] = DateTime.Now.AddDays(10 - rnd.Next(20));
            dt.Rows.Add(row);
        }
        dataGridView1.DataSource = dt;
0

You should use DataFormatString as {0:MM/dd/yyyy} on the column to get desired date format.

Anup Sharma
  • 2,053
  • 20
  • 30
  • I'm sorry. My bad. I didn't realize it was win form and not web form. Gridview name confused me. Any ways, the code is perfect and should work. However you can also try to pass the index of the column instead of name while setting the format. – Anup Sharma Mar 25 '15 at 06:10
0

Make sure that the date stored in the database is in DateTime or Date DataType.

If the datatype of that field is not datetime or date then it could not be formatted using that format. Either convert the datatype into datetype in table schema or convert the date in sql query.

Select Cast(entry_datetime As DateTime)...

If the field datatype is datetime and still it is showing that result then you can format the date in your sql query directly.

Select CONVERT(VARCHAR(10), entry_datetime, 101) As EntryDate,....
Shell
  • 6,818
  • 11
  • 39
  • 70
0
dataGridView1.Columns[0].FormatString = "{0:dd.MM.yyyy}";

Just replace your column index[0,1,2...etc] and get your desired result!

0

for example.

  gridView.DefaultCellStyle.Format = "dd/MM/yyyy";

try this

  gridView.Columns[3].DefaultCellStyle.Format = "dd/MM/yyyy";
Nitin...
  • 349
  • 3
  • 7