We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

MonthCalendar celltype issue

Based on a previous subject :"How do I supress keystrokes in a month calendar cell" at your forum, I added some more code to implement some other keystroke for day ++, or day --. Please try the example code attached, and let me know why the second key "-" or "+" will get the error " object reference not set to an instance of an object" at DateTime dat = (DateTime)cc.Renderer.ControlValue;". Thanks.

Forum_407351.zip

2 Replies

AD Administrator Syncfusion Team March 16, 2006 09:45 AM UTC

Hi Hui, The mentioned issue is caused because of a null value (cc.Renderer.ControlValue) is tried to cast as DateTime type. To get the display text here, the Control.Text must be used. Below is a code snippet. void gridControl1_CurrentCellValidateString(object sender, GridCurrentCellValidateStringEventArgs e) { GridCurrentCell cc = this.gridControl1.CurrentCell; if (cc.Renderer.StyleInfo.CellType == "MonthCalendar") if (e.Text.IndexOf("t") > -1) { e.Cancel = true; cc.CancelEdit(); cc.Renderer.Control.Text = DateTime.Now.ToString(); this.gridControl1.RefreshRange(cc.RangeInfo, true); } else if(e.Text.IndexOf("+") > -1 || e.Text.IndexOf("-") > -1) { e.Cancel = true; if (cc.Renderer.Control.Text.Trim() != "") { cc.CancelEdit(); DateTime dat = Convert.ToDateTime(cc.Renderer.Control.Text); int diff = e.Text.IndexOf("+") > -1 ? 1 : -1; cc.Renderer.Control.Text = dat.AddDays(diff).ToString("d"); dt.Rows[cc.RowIndex - 1][cc.ColIndex - 1] = cc.Renderer.Control.Text; this.gridControl1.RefreshRange(cc.RangeInfo, true); } } } Regards, Calvin.


HZ Hui Zhong March 16, 2006 02:53 PM UTC

Thanks, Calvin. It worked. However, I can''t figure out why cc.Renderer.ControlValue is null though there is validate date in the cell. >Hi Hui, > >The mentioned issue is caused because of a null value (cc.Renderer.ControlValue) is tried to cast as DateTime type. To get the display text here, the Control.Text must be used. Below is a code snippet. > > > void gridControl1_CurrentCellValidateString(object sender, GridCurrentCellValidateStringEventArgs e) > { > GridCurrentCell cc = this.gridControl1.CurrentCell; > if (cc.Renderer.StyleInfo.CellType == "MonthCalendar") > if (e.Text.IndexOf("t") > -1) > { > e.Cancel = true; > cc.CancelEdit(); > cc.Renderer.Control.Text = DateTime.Now.ToString(); > this.gridControl1.RefreshRange(cc.RangeInfo, true); > > } > else > if(e.Text.IndexOf("+") > -1 || e.Text.IndexOf("-") > -1) > { > e.Cancel = true; > if (cc.Renderer.Control.Text.Trim() != "") > { > cc.CancelEdit(); > DateTime dat = Convert.ToDateTime(cc.Renderer.Control.Text); > int diff = e.Text.IndexOf("+") > -1 ? 1 : -1; > cc.Renderer.Control.Text = dat.AddDays(diff).ToString("d"); > dt.Rows[cc.RowIndex - 1][cc.ColIndex - 1] = cc.Renderer.Control.Text; > this.gridControl1.RefreshRange(cc.RangeInfo, true); > } > > } > } > >Regards, >Calvin.

Loader.
Live Chat Icon For mobile
Up arrow icon