Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Format DateTime value in DataGridView column header cell?
 

Format DateTime value in DataGridView column header cell?

I need to hold a DateTime value in the HeaderCell of a text column, but format it as dd-MMM:

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
DateTime d = DateTime.Today;
col.HeaderCell.Value = d
col.HeaderCell.Style.Format = "dd-MMM";
col.Name = d.ToShortDateString();
col.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvMyGrid.Columns.Add(col);


All I get are blank column header cells. What am I missing?
kaborka  Monday, October 05, 2009 11:55 PM
Hi,
Try This
 DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            DateTime d = DateTime.Today;
            col.HeaderCell.Value = d;           
            col.HeaderText = d.ToString("dd-MMM");
            col.SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns.Add(col);
i hope it will help you
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Tuesday, October 06, 2009 4:50 AM
That solution did not work.  Assigning col.HeaderText also replaces the col.HeaderCell.Value.  I guess I'll have to handle CellPainting to format the HeaderCell if the format string doesn't work.
kaborka  Wednesday, October 07, 2009 1:29 AM
That solution did not work.  Assigning col.HeaderText also replaces the col.HeaderCell.Value.  I guess I'll have to handle CellPainting to format the HeaderCell if the format string doesn't work.

Hi kaborka,

Yes, you are right, we have to draw by ourselves. This is a similar thread:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/34dfa657-e7db-4727-8693-74e411bec1de

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  23 hours 50 minutes ago
Hi Gnanadurai,

I have read your reply again and find your reply can also be a solution. We can store the DataTiem value in the Tag property of the column and get it from Tag when we need to use it.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  22 hours 58 minutes ago
Hi,
Try This
 DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            DateTime d = DateTime.Today;
            col.HeaderCell.Value = d;           
            col.HeaderText = d.ToString("dd-MMM");
            col.SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns.Add(col);
i hope it will help you
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Tuesday, October 06, 2009 4:50 AM
That solution did not work.  Assigning col.HeaderText also replaces the col.HeaderCell.Value.  I guess I'll have to handle CellPainting to format the HeaderCell if the format string doesn't work.
kaborka  Wednesday, October 07, 2009 1:29 AM
Hi,
You want to show datetime in header cell.its working for me .
 DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            DateTime d = DateTime.Today;
            col.HeaderCell.Value = d.ToString("dd-MMM");         
            col.SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns.Add(col);
            dataGridView1.Columns.Add(column1);
if you want more help let me know.and tell me the exact scenario you want to show the datetime value in dgv column header.i hope it will help you.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, October 07, 2009 4:08 AM
In my app, the HeaderCell.Value must contain a DateTime object, not a string.  It must format as I described, dd-MMM.  Setting HeaderText replaces the DateTime with the string.

Don't worry, I have a workaround.  I was just disappointed that setting HeaderCell.Style.Format did not work.
kaborka  Wednesday, October 07, 2009 4:31 PM
That solution did not work.  Assigning col.HeaderText also replaces the col.HeaderCell.Value.  I guess I'll have to handle CellPainting to format the HeaderCell if the format string doesn't work.

Hi kaborka,

Yes, you are right, we have to draw by ourselves. This is a similar thread:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/34dfa657-e7db-4727-8693-74e411bec1de

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  23 hours 50 minutes ago
That solution did not work.  Assigning col.HeaderText also replaces the col.HeaderCell.Value.  I guess I'll have to handle CellPainting to format the HeaderCell if the format string doesn't work.

Hi kaborka,

Yes, you are right, we have to draw by ourselves. This is a similar thread:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/34dfa657-e7db-4727-8693-74e411bec1de

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

Hi Alan,
He want to show datatime value in the header cell as a datatime datatype with custom datetime format("dd-MMM").
as a string only we can able to draw.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  23 hours 5 minutes ago
In my app, the HeaderCell.Value must contain a DateTime object, not a string.  It must format as I described, dd-MMM.  Setting HeaderText replaces the DateTime with the string.

Don't worry, I have a workaround.  I was just disappointed that setting HeaderCell.Style.Format did not work.

Hi,
You want to do some calculation with header cell at that time you can able to convert as a datetime value.so ther is no problem with showing as a string value.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  23 hours 3 minutes ago
Hi Gnanadurai,

I have read your reply again and find your reply can also be a solution. We can store the DataTiem value in the Tag property of the column and get it from Tag when we need to use it.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  22 hours 58 minutes ago
As it turns out, I need to handle CellPainting in this grid anyway, to emulate a multiline textbox cell with clipping.  Storing the DateTime for the column in the Tag is also an adequate solution.

Thanks to you both.
kaborka  11 hours 48 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Checking the data type of dataGridView cell content
• VS2005: Inherited Base forms and control event issues
• How to change the DataSet Designer
• Is it possible to 'hook up' my exception to the DataError event of a DataGridView?
• Textbox and DisplayMember in 2005?
• Order of columns in a DataGridView
• datagridview +
• Showing dates 01-01-1900 00:00:00 ?????
• How can i avoid the Unicode File conversion wizard at the time of opening up the document? [C#.net---Ms-Access---MS-Word]
• Trying a Top 10 With Datagridview Bounded to a DataSet