I have a user control in which a grid receives data using an ObjectDataSource. All columns in the grid are sorted. If the user clicks on a specific column name, the list is sorted based on that column (List.Sort (sortColumn)).
I ran into a problem when one of the columns has an empty / zero value in its field. The strA.CompareTo (strB) comparison string fails with "Object reference not set to object instance" when strA / strB is null or both are null.
Be that as it may, I turned it on! string.IsNullOrEmpty () for strA and strB to avoid the null refernce exception. However, it does not sort the grid.
Below is a snippet of code.
int IComparer<MyWorklistItem>.Compare(MyWorkItem x, MyWorkItem y)
{
int sortValue = 1;
if (this.strSortField.Length == 0)
{
return 0;
}
MyWorkItem item1 = this.blnSortDesc ? y : x;
MyWorkItem item2 = this.blnSortDesc ? x : y;
PropertyInfo property1 = item1.GetType().GetProperty(this.strSortField);
PropertyInfo property2 = item2.GetType().GetProperty(this.strSortField);
string strA = (string)property1.GetValue(item1, null);
string strB = (string)property2.GetValue(item2, null);
if (!string.IsNullOrEmpty(strA) && !string.IsNullOrEmpty(strB))
{
sortValue = strA.CompareTo(strB);
}
return sortValue;
}
, .
. VS 2005, LINQ.
.
,
Sriram