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 - Proposed As Answer byGnanadurai Tuesday, October 06, 2009 4:53 AM
- Unproposed As Answer bykaborka Wednesday, October 07, 2009 1:29 AM
- Proposed As Answer byAland LiMSFT, Moderator22 hours 57 minutes ago
- Marked As Answer byAland LiMSFT, Moderator2 hours 55 minutes ago
-
|
| 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. - Proposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Marked As Answer byAland LiMSFT, Moderator2 hours 55 minutes ago
-
|
| 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-74e411bec1deRegards, 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. - Proposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Unproposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Proposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Marked As Answer bykaborka 11 hours 48 minutes ago
-
|
| 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.- Proposed As Answer byAland LiMSFT, Moderator22 hours 57 minutes ago
- Marked As Answer byAland LiMSFT, Moderator2 hours 55 minutes ago
-
|
| 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 - Proposed As Answer byGnanadurai Tuesday, October 06, 2009 4:53 AM
- Unproposed As Answer bykaborka Wednesday, October 07, 2009 1:29 AM
- Proposed As Answer byAland LiMSFT, Moderator22 hours 57 minutes ago
- Marked As Answer byAland LiMSFT, Moderator2 hours 55 minutes ago
-
|
| 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. - Proposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Marked As Answer byAland LiMSFT, Moderator2 hours 55 minutes ago
-
|
| 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-74e411bec1deRegards, 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. - Proposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Unproposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Proposed As Answer byAland LiMSFT, Moderator23 hours 49 minutes ago
- Marked As Answer bykaborka 11 hours 48 minutes ago
-
|
| 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.- Proposed As Answer byAland LiMSFT, Moderator22 hours 57 minutes ago
- Marked As Answer byAland LiMSFT, Moderator2 hours 55 minutes ago
-
|
| 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 |