Thursday, September 25, 2008

gridItem.OwnerTableView.DataKeyValues NullReferenceException

GridDataItem gridItem = e.Item as GridDataItem;
gridItem["ColumnName"].ToolTip = "CustomerID: " + gridItem.OwnerTableView.DataKeyValues[gridItem.ItemIndex][ "ColumnName2"].ToString();

While trying to assign a tooltip to a Telerik Radgrid cell with the above code (courtesy of the Telerik documentation) I kept getting a NullReferenceException. For some reason i could not ascertain the gridItem.ItemIndex kept coming up 0. I circumvented the problem by casting the GridDataItem to a DataRowView and using the named index of the row to get the value. Not sure why Telerik tried to make it so complicated...

GridDataItem gridItem = e.Item as GridDataItem;
DataRowView row = (DataRowView)gridItem.DataItem;
gridItem["ColumnName"].ToolTip = row["ColumnName2"].ToString();