Click here to Skip to main content
15,917,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all. I want to display only date( without time ) in my datagridview c#.
Any help would be appreciated.
I have tried the following:

C#
dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";


This is my method:
C#
private void bindListToGridView1(List<Item> list)
        {
            try
            {
                dataGridView1.AllowUserToAddRows = false;
                ListtoDataTableConverter converter = new ListtoDataTableConverter();
                DataTable dt2 = converter.ToDataTable(listToPresent);

                dataGridView1.DataSource = null;

                using (dt2)
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.ColumnCount = 3;
                    
                    dataGridView1.Columns[0].Name = "id";
                    dataGridView1.Columns[0].HeaderText = "ID";
                    dataGridView1.Columns[0].DataPropertyName = "id";

                    dataGridView1.Columns[1].Name = "itemName";
                    dataGridView1.Columns[1].HeaderText = "Item Name";
                    dataGridView1.Columns[1].DataPropertyName = "itemName";

                    dataGridView1.Columns[2].Name = "productionDate";
                    dataGridView1.Columns[2].HeaderText = "Production Date";
                    dataGridView1.Columns[2].DataPropertyName = "productionDate";
                    dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";

                    dataGridView1.DataSource = dt2;
                }
            }
            catch (Exception ex)
            {
            }
        }


What I have tried:

C#
dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";
Posted
Updated 22-Mar-16 17:25pm
Comments
FARONO 22-Mar-16 16:30pm    
Isn't it possible to format the date in the "ListtoDataTableConverter"? (so in advance, with ToString("..."))? And the Format, doesn't it work without the quote's, so "yyyy/MM/dd"?
plazzasele 22-Mar-16 16:52pm    
this was the first I tried: dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy/MM/dd"; it was showing date and time, then i searched for this and found an answer tith the car ' '
plazzasele 22-Mar-16 18:16pm    
I am using this class from this articel, And i actually don't know how to change it to only dispaly the date. http://www.c-sharpcorner.com/UploadFile/1a81c5/list-to-datatable-converter-using-C-Sharp/

This piece of code will give you the date in the format you are looking for.
C#
string myDate = DateTime.Now.ToString("yyyy/MM/dd")

The best place to convert the date to the format you want will be in the method where you convert List to Datatable, so you don't have to do all these when the data is bound to the datagrid.

For this you have to modify the converter method, where you identify the property by name and change the format.

Hope this helps.
 
Share this answer
 
I got help that solved my problem and i want to share the solution.
I changed this line:
C#
dataTable.Columns.Add(prop.Name, prop.PropertyType);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900