DataGridViewCell value is updating but new value is not displayed on screen

My first post here - hello :)
I have a DataGridView with one particular cell that allows the user to

adjust the day of the current week by simply focusing on that cell and

scrolling the mouse wheel up and down. The code works no problem, the

value is indeed updated but it does not show the new value until I

change focus to another cell.


private void dataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
try
{
DataGridViewRow dgvRow = dataGridView1.CurrentRow;
if (dgvRow.Cells[clockDateDataGridViewTextBoxColumn.Name].Selected)
{
//increase day component of the ClockDate up to a maximum of (Start of this week + 6)
DateTime minDate = formClasses.getFirstDayOfWeek(monthCalendar1.SelectionStart);
DateTime maxDate = formClasses.getLastDayOfWeek(monthCalendar1.SelectionStart);
DateTime clockDate = DateTime.Parse(dgvRow.Cells[clockDateDataGridViewTextBoxColumn.Name].Value.ToString());
if (e.Delta >= 120 && clockDate.CompareTo(maxDate) < 0)
{
//add 1 day to the current value
dgvRow.Cells[clockDateDataGridViewTextBoxColumn.Name].Value = clockDate.AddDays(1).ToShortDateString();
}
else if (e.Delta <= -120 && clockDate.CompareTo(minDate) > 0)
{
//subtract 1 day from the current value
dgvRow.Cells[clockDateDataGridViewTextBoxColumn.Name].Value = clockDate.AddDays(-1).ToShortDateString();
}
}
}

catch (....)


When the use scrolls the mouse up or down a notch I want the date in

the cell to be updated then, I don't want it to display the original

value until moving focus away. The exact same problem happens with another cell in my DataGridView where I want to add 15 minutes onto the time after scrolling the wheel upwards. There's no cell refresh method of sorts

that I can immediately see anywhere - any ideas?

Another thing - when I scroll once upwards with the mousewheel, it fired the _MouseWheel event twice so I always get 2 days added on. I could make it add half a day on each scroll, but would rather sort it properly - why is the event being fired twice?

[2522 byte] By [smickell] at [2007-12-22]
# 1
The Datagridview has an InvalidateCell method which will force a cell to be repainted. Maybe after you change the value invalidate the cell to get it to refresh its value.
KenTucker at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Thanks for the reply Ken. Your suggestion for the InvalidateCell() did not work (I also tried the same for row and column) but inspired me to do some more searching, and I found this post:
http://www.eggheadcafe.com/aspnet_answers/NETFrameworkNETWindowsForms/Apr2006/post26457558.asp

It turns out that I should modify the underlying data source and this indeed worked correctly without me having to repaint or invalidate anything.
The correct line turned out to be:
this.dataSet1.Timesheet.Rows[updateID]["TimesheetDate"] = timesheetDate.AddDays(1).ToShortDateString();

HOWEVER - this is still not of any use to me when I am in the middle of creating a new row - I get an exception saying that the row in question does not exist, which is obviously because I haven't saved it yet. I don't want the user to have to save the record before this functionality is available. So how can I change this to modify a row that hasn't been saved to the database yet?

*edit - I must also add that I didn't have to go to the underlying datasource to set DataGridViewCell values on the _DefaultValuesNeeded event:
private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[timesheetDateDataGridViewTextBoxColumn.Name].Value = monthCalendar1.SelectionStart.ToString();
e.Row.Cells[startTimeDataGridViewTextBoxColumn.Name].Value = STARTTIME;
e.Row.Cells[endTimeDataGridViewTextBoxColumn.Name].Value = ENDTIME;
e.Row.Cells[hoursTotalDataGridViewTextBoxColumn.Name].Value = HOURSTOTAL;
}

This sets the values of the cells once I create a new row without a problem. Why does it work here but not after this?

smickell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
Well after many hours of posting in various message boards I found a solution to part of the problem.
Invalidate()

methods did not work, changing the cell.Value didn't, and changing the

underlying datasource only worked if the row was already saved to the

database.
I found the .RefreshEdit() method of the DataGridView and

that solved the refresh problem with a single line:

this.dataGridView1.RefreshEdit();

But now, I still haven't discovered why the _Mousewheel event is being fired twice for every single scroll of the mouse?

smickell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...