Hi guys,
Problem:
DataGridView with entity collection binded. Nullable DateTime fields are elements of entity.
DataGridView is editable and for DateTime fileds is used custom DateTimePicker control as custom DataGrid columns controls. Columns are sortable as entity collection supports sorting. Tried to search forum but only similar problems (so maybe the wrong version I have
)
Everything works fine until.... First I fetch collection with NULL value in DB for DateTime fileds. Then select DateTime cell and set some date...try to sort it works fine.
Then I set the same field to null value. In custom DateTimeCell value to null is set with the code below:
protected override bool SetValue(int rowIndex, object value)
{
...
// dataSourceRow.SetNewFieldValue(this.OwningColumn.DataPropertyName, null);
MethodInfo methodInfo = dataSourceRowType.GetMethod("SetNewFieldValue", new Type[] { typeof(string), typeof(object) });
methodInfo.Invoke(dataSourceRow, new object[] { this.OwningColumn.DataPropertyName, null });
// Entity.IsNull = true;
Type entityFieldType = fieldInfo.GetType();
PropertyInfo entityFieldIsNull = entityFieldType.GetProperty("IsNull");
entityFieldIsNull.SetValue(fieldInfo, true, null);
...
}
Update of that entity collection works fine... but before I'm updating DB hit sort on DataGridView (HeaderClick).
Then app crash with this exception dump:
System.ArgumentNullException: Key cannot be null.
Parameter name: key
at System.Collections.Hashtable.ContainsKey(Object key)
at SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2.Sort(PropertyDescriptor descriptor, ListSortDirection direction, IComparer comparerToUse)
at SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2.ApplySort(PropertyDescriptor property, ListSortDirection direction)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
at System.Windows.Forms.DataGridView.SortInternal(IComparer comparer, DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
at System.Windows.Forms.DataGridView.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
at System.Windows.Forms.DataGridView.OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
LLBL ver. : 1.0.2005.1 Final (Novembar 2005), NET 2.0.
I have tested to change the code for handling null value in cell setting to null but then exception setting DateTime to DBNull.Value that breaks (thats OK and expected behaviour - DateTime can't be DBNull.Value).
Setting to DateTime.MinValue everything works fine but update breaks (SqlDateTime overflow. Must be between 1/1/1753 ...) with SQL date time min value.
Any suggestions could help. THX!