How do I display MM/dd/yyyy (no time) in my DataGridView column?

99封情书 提交于 2019-12-01 11:12:03

问题


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?

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";`

回答1:


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

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




回答2:


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

dataGridView1.Columns["dateReceived"].DefaultCellStyle.Format = "MM/dd/yyyy";



回答3:


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;



回答4:


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




回答5:


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,....



回答6:


dataGridView1.Columns[0].FormatString = "{0:dd.MM.yyyy}";

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




回答7:


for example.

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

try this

  gridView.Columns[3].DefaultCellStyle.Format = "dd/MM/yyyy";


来源:https://stackoverflow.com/questions/29246840/how-do-i-display-mm-dd-yyyy-no-time-in-my-datagridview-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!