Reputation: 15039
Im getting from database 2 date fields in string format like:
"yyyy.mm.dd"
Then, In my form I want to add DataBinding to the 2 DatePickers that will show these dates like below:
dtStart.DataBindings.Add(new Binding("Value", _ds.Tables["invoice"], "startdate"));
dtEnd.DataBindings.Add(new Binding("Value", _ds.Tables["invoice"], "enddate"));
The problem right now is that when I change the date from the DatePicker then the Value will be a Date and what I need is to update the value but in the same string format "yyyy.mm.dd".
Any clue how can I force to do that?
Thanks a lot
Upvotes: 0
Views: 2920
Reputation: 63317
If your DataGridViewColumn startdate
is type of string
. You can add code to the Parse
event handler of a Binding
to format the value before updating back to the DataSource:
Binding bind = new Binding("Value", _ds.Tables["invoice"], "startdate");
bind.Parse += (s,e) => {
e.Value = ((DateTime)e.Value).ToString("yyyy.mm.dd");
};
dtStart.DataBindings.Add(bind);
//Do the same for dtEnd
However, I recommend setting your DataGridViewColumn data type
as DateTime
. Then you won't need any custom Parsing
and just set the format for the cells in that column like this:
dataGridView1.Columns["startdate"].DefaultCellStyle.Format = "yyyy.mm.dd";
Upvotes: 1