Forum  General Visual ...  Visual WebGui v...  How do I show date without time in a DataGridView column?
Previous Previous
 
Next Next
New Post 12/16/2011 3:55 AM
Resolved
  tejuguasu
21 posts
No Ranking


How do I show date without time in a DataGridView column? 

Hello all.

My DataTable, populated from a DB table, has a DateTime field.

When assigning it as the DataGridView dataSource, the date column shows date and time.

How do I hide or truncate the date field, thus showing only the date?

I even tried to create a new column and truncate at the expression, but couldn't find any command for this purpose

 
New Post 12/18/2011 2:43 PM
  palli
14339 posts
1st Level Poster




Re: How do I show date without time in a DataGridView column? 

Hi tejunguasu,

You can actually do this through the designer. Open the Columns collection for the DataGridView and expand the DefaultCellStyle and set the Format property. You can even select it from a list.

Using the designer assumes that you are not autopopulating the columns collection of your DataGridView. If you are autopopulating, you can examin the designer generated code when you add a column and set it's format.

The following code uses a prepopulated 3 column DataGridView bound to a DataTable with three DateTime columns and using the above advice to set their formatting dynamically.

 

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable DT = new DataTable();
            DT.Columns.Add("DT", typeof(DateTime));
            DT.Columns.Add("JustDate", typeof(DateTime));
            DT.Columns.Add("JustTime", typeof(DateTime));
            for (int i = 0; i < 10; i++)
                DT.Rows.Add(DateTime.Now, DateTime.Now, DateTime.Now);
 
 
 
            this.dataGridView1.AutoGenerateColumns = false;
            FormatColumns(this.dataGridView1);
            this.dataGridView1.DataSource = DT;
        }
 
        private void FormatColumns(DataGridView DGW)
        {
            DataGridViewCellStyle cStyle = new DataGridViewCellStyle();
 
            // Second column will be formatted as DateOnly
            cStyle.Format = "d";
            cStyle.NullValue = null;
            this.dataGridView1.Columns[1].DefaultCellStyle = cStyle;
 
            // Third column will be formatted as TimeOnly
            cStyle = new DataGridViewCellStyle();
            cStyle.Format = "T";
            cStyle.NullValue = null;
            this.dataGridView1.Columns[2].DefaultCellStyle = cStyle;
 
        }

Hope this helps,

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
Previous Previous
 
Next Next
  Forum  General Visual ...  Visual WebGui v...  How do I show date without time in a DataGridView column?
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS