locked
Format DateTime value in DataGridView column header cell? RRS feed

  • Question

  • 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?
    Monday, October 5, 2009 11:55 PM

Answers

  • 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 by Gnanadurai Tuesday, October 6, 2009 4:53 AM
    • Unproposed as answer by kaborka Wednesday, October 7, 2009 1:29 AM
    • Proposed as answer by Aland Li Thursday, October 8, 2009 7:00 AM
    • Marked as answer by Aland Li Friday, October 9, 2009 3:01 AM
    Tuesday, October 6, 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 by Aland Li Thursday, October 8, 2009 6:07 AM
    • Marked as answer by Aland Li Friday, October 9, 2009 3:01 AM
    Wednesday, October 7, 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.
    • Proposed as answer by Aland Li Thursday, October 8, 2009 6:07 AM
    • Unproposed as answer by Aland Li Thursday, October 8, 2009 6:08 AM
    • Proposed as answer by Aland Li Thursday, October 8, 2009 6:08 AM
    • Marked as answer by kaborka Thursday, October 8, 2009 6:09 PM
    Thursday, October 8, 2009 6:07 AM
  • 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 by Aland Li Thursday, October 8, 2009 7:00 AM
    • Marked as answer by Aland Li Friday, October 9, 2009 3:01 AM
    Thursday, October 8, 2009 6:59 AM

All replies

  • 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 by Gnanadurai Tuesday, October 6, 2009 4:53 AM
    • Unproposed as answer by kaborka Wednesday, October 7, 2009 1:29 AM
    • Proposed as answer by Aland Li Thursday, October 8, 2009 7:00 AM
    • Marked as answer by Aland Li Friday, October 9, 2009 3:01 AM
    Tuesday, October 6, 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 by Aland Li Thursday, October 8, 2009 6:07 AM
    • Marked as answer by Aland Li Friday, October 9, 2009 3:01 AM
    Wednesday, October 7, 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
    Wednesday, October 7, 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.
    Wednesday, October 7, 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.
    • Proposed as answer by Aland Li Thursday, October 8, 2009 6:07 AM
    • Unproposed as answer by Aland Li Thursday, October 8, 2009 6:08 AM
    • Proposed as answer by Aland Li Thursday, October 8, 2009 6:08 AM
    • Marked as answer by kaborka Thursday, October 8, 2009 6:09 PM
    Thursday, October 8, 2009 6:07 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.

    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
    Thursday, October 8, 2009 6:52 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.

    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
    Thursday, October 8, 2009 6:54 AM
  • 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 by Aland Li Thursday, October 8, 2009 7:00 AM
    • Marked as answer by Aland Li Friday, October 9, 2009 3:01 AM
    Thursday, October 8, 2009 6:59 AM
  • 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.
    Thursday, October 8, 2009 6:08 PM