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
close icon

Nullable date column in DataGrid

Ahhh. I just wrote a long message on this subject but lost it so i''ll be more concise at the expense of explaining what i''ve already tried. I need a nullable column in a datagrid where i can select a date. Dont necessarily care about being able to type a date in. I need it to be impossible to enter incorrect data but I do need to permit a null value. I cant find any example that allows me to enter in a date but that also prevents user entry. If i permit user entry it is possible to enter (obviously) something that isnt a date. In some of your examples there is a messagebox displayed if there is an error but I cant see how to do this. I have a global exception handler which only gets passed an ''ArgumentException''. Rather than an error message on a failed parse i''d rather just disallow manual entry altogether. Is this a simple property i''m missing or something more complex? Thanks -simon -simon

6 Replies

AD Administrator Syncfusion Team April 30, 2004 11:03 AM UTC

I was able to do this by cancelling the StartEditing in the datetime column, and also had to move the value explicitly into the cell when the calendar closed. Below are the handlers assuming teh column is 2.
private void gridDataBoundGrid1_CurrentCellStartEditing(object sender, CancelEventArgs e)
{
	GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
	if(cc.ColIndex == 2)
		e.Cancel = true;
}
private void gridDataBoundGrid1_CurrentCellCloseDropDown(object sender, PopupClosedEventArgs e)
{
	GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
	if(cc.ColIndex == 2)
		this.gridDataBoundGrid1.CurrentCell.ConfirmChanges();
}

            


AD Administrator Syncfusion Team April 30, 2004 07:29 PM UTC

great thanks. couple more things on the same topic. i have another column in this same DataTable which is a numeric value in a combobox. the database column is a nullable int field. the datasource for the column is initialized with the following to constrain it to a certain value: gbcPercentDone.StyleInfo.DataSource = new int[] { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; null values are valid and correctly displayed in the table through load and save operations however if you click on a value in the percent due list and hit delete then you get the following exception : System.ArgumentException: System.FormatException: Input string was not in a correct format. at System.Number.ParseSingle(String s, NumberStyles style, NumberFormatInfo info) at System.Single.Parse(String s, NumberStyles style, IFormatProvider provider) at System.Convert.ToSingle(String value, IFormatProvider provider) at System.String.System.IConvertible.ToSingle(IFormatProvider provider) at System.Convert.ToSingle(Object value) at System.Data.Common.SingleStorage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn''t store <> in PercentDone Column. Expected type is Single. at Syncfusion.Windows.Forms.Grid.GridModelDataBinder.SaveCellInfo(GridSaveCellInfoEventArgs e) at Syncfusion.Windows.Forms.Grid.GridModelDataBinder.Syncfusion.Windows.Forms.Grid.IGridModelDataProvider.SaveCellInfo(GridSaveCellInfoEventArgs e) at Syncfusion.Windows.Forms.Grid.GridModel.SetCellInfo(Int32 rowIndex, Int32 colIndex, GridStyleInfo style, StyleModifyType modifyType, Boolean dontRaiseSaveCellInfoEvent, Boolean copyReferenceOnly) at Syncfusion.Windows.Forms.Grid.GridModel.SetCellInfo(Int32 rowIndex, Int32 colIndex, GridStyleInfo style, StyleModifyType modifyType) at Syncfusion.Windows.Forms.Grid.GridModel.ChangeCells(GridRangeInfo range, GridStyleInfo[] cellsInfo, StyleModifyType modifyType) at Syncfusion.Windows.Forms.Grid.GridModel.Syncfusion.Windows.Forms.Grid.IGridVolatileDataContainer.ChangeCell(Int32 rowIndex, Int32 colIndex, GridStyleInfo style) at Syncfusion.Windows.Forms.Grid.GridVolatileData.set_Item(Int32 rowIndex, Int32 colIndex, GridStyleInfo value) at Syncfusion.Windows.Forms.Grid.GridStyleInfoIdentity.OnStyleChanged(StyleInfoBase style, StyleInfoProperty sip) at Syncfusion.Styles.StyleInfoBase.OnStyleChanged(StyleInfoProperty sip) at Syncfusion.Windows.Forms.Grid.GridStyleInfo.OnStyleChanged(StyleInfoProperty sip) at Syncfusion.Styles.StyleInfoBase.SetValue(StyleInfoProperty sip, Object value) at Syncfusion.Windows.Forms.Grid.GridCellModelBase.ApplyFormattedText(GridStyleInfo style, String text, Int32 textInfo) at Syncfusion.Windows.Forms.Grid.GridComboBoxCellModel.ApplyFormattedText(GridStyleInfo style, String text, Int32 textInfo) at Syncfusion.Windows.Forms.Grid.GridStyleInfo.ApplyFormattedText(String text, Int32 textInfo) at Syncfusion.Windows.Forms.Grid.GridTextBoxCellRenderer.OnSaveChanges() at Syncfusion.Windows.Forms.Grid.GridCurrentCell.ConfirmChanges(Boolean closeDropDown) I have managed to get around this with the following code. You will see there is code to handle the pressing of the delete key for both the datetime and int fields described above. When you push delete on a currentcell it looks to see what cell you''re on and sets that column to null. The date field particularly needs to be nullable. the only problem here is while this works great for existing data it doesnt work for data you''ve just entered. That is to say if you select a date from the dropdown and then immediately try to delete it you cant. You have to click on a different ROW and then click back on the cell in question. You cant click on a different column in the same row - you have to click on a different row. I''ve tried setting the column to null AFTER i try to confirm changes but tht doesnt make a difference. So to recap the problem is if you click the datetime column or the combobox int column to set a value and then try to delete it with the below keydown event you have to first change to a different ROW and switch back again to be able to delete it. I assume this is some currencymanger related issue. ANy ideas? private void gridTasks_CurrentCellKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { GridCurrentCell cc = this.gridTasks.CurrentCell; // check for delete pressed if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back) { // delete pressed in date deleted column if (gridTasks.GridBoundColumns[cc.ColIndex-1] == gbcDueDate) { DataView dv = (DataView) gridTasks.DataSource; TSEDataSetTMDB.tasksRow task = (TSEDataSetTMDB.tasksRow)((DataRowView) dv[cc.RowIndex-1]).Row; task.SetDueNull(); // have tried different combinations of these lines - makes no difference gridTasks.CurrentCell.ConfirmChanges(); gridTasks.CurrentCell.EndEdit(); e.Handled = true; } // delete pressed in percent done column if (gridTasks.GridBoundColumns[cc.ColIndex-1] == gbcPercentDone) { DataView dv = (DataView) gridTasks.DataSource; TSEDataSetTMDB.tasksRow task = (TSEDataSetTMDB.tasksRow)((DataRowView) dv[cc.RowIndex-1]).Row; task.SetPercentDoneNull(); // have tried different combinations of these lines - makes no difference gridTasks.CurrentCell.EndEdit(); gridTasks.CurrentCell.ConfirmChanges(); e.Handled = true; } } } ANy thoughts? -simon


AD Administrator Syncfusion Team April 30, 2004 09:54 PM UTC

What version are you using? I do not see this problem in the 2.0 code base in the attached sample. Do you? WindowsApplication9_7227.zip


AD Administrator Syncfusion Team May 4, 2004 04:46 PM UTC

Thanks for the app. I''ve modified it to show the issue. Instructions appear when it is run. I am using 2.0.5.0 ANy ideas how i can get to delete the date cell without moving the currencymanager off the row. Thanks WindowsApplication9_5895.zip


AD Administrator Syncfusion Team May 4, 2004 07:08 PM UTC

In addition to calling currentcell.ConfirmChanges in CurrentCellCloseDropDown, also try calling grid.Binder.EndEdit().
private void gridDataBoundGrid1_CurrentCellCloseDropDown(object sender, PopupClosedEventArgs e) {
	GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
	this.gridDataBoundGrid1.CurrentCell.ConfirmChanges();
	this.gridDataBoundGrid1.Binder.EndEdit();
}


AD Administrator Syncfusion Team May 4, 2004 08:35 PM UTC

Great. that did it. as you can see from one of my previous posts i did try to do that, but i tried it in the keydown method to try and accept the change before immediately deleting it. this didnt seem to work, but adding it to the dropdown closed code works great. t thanks!

Loader.
Live Chat Icon For mobile
Up arrow icon