Click here to Skip to main content
16,022,418 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,

These is really confusing me.
I need to store a DateTime object to the column header of a DataGridView so I can refer to the object later.

Look at this code.
C#
DataGridViewColumn s = new DataGridViewColumn()

s.HeaderCell.Value = new DateTime.Now;


I am expecting the column header caption to have the value of the ToString() method of the date time object but instead its blank.

Now if I set s.HeaderText with a string value. It overwrites the object I assigned to HeaderCell to the string value.

Any idea where I went wrong?
Posted
Updated 11-Oct-11 10:04am
v3
Comments
markypm 11-Oct-11 16:15pm    
why the downvote..? i need help anyone?

1 solution

Does that code compile?

I do think you cannot assign a DateTime object to a HeaderCell object.
What you can do is to assign the DateTime value to the HeaderCell value like this
C#
s.HeaderCell.Value = DateTime.Now.ToString();

If you want to retrieve the information you can use
C#
DateTime headerTime = DateTime.Parse(s.HeaderCell.Value);
 
Share this answer
 
v2
Comments
markypm 11-Oct-11 16:03pm    
hi, thanks for the reply and for editing the code. yes sorry it is HeaderCell.Value.. It accepts an object so it does compile and properly store the DateTime object. But it does not display it as caption. where as any other function that accepts an object automatically takes the ToString() returned value.. or maybe I'm approaching it the wrong way?

If I do it as you suggested then the value will be a string and not a datetime object. i need to store a datetime object cause later in the code i need to be able to get the object it holds.
André Kraak 11-Oct-11 16:15pm    
As far as I can determine this is not possible.

But as I suggested you can parse back the date when you need to use the DateTime object.
markypm 11-Oct-11 16:29pm    
yeh that's how im doing it right now i just don't understand why it acts that way. thanks anyway!

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