I am using a heirarchical grid and there are times the childGrid could be null. how to check that condition?
GridViewRow
grdRow = (GridViewRow)grdCell.ParentRow;
RadGridView
childGrid = grdRow.ChildrenOfType<RadGridView>().FirstOrDefault();
Please let me know
Thanks
Sathish.K
3 Answers, 1 is accepted
It will be better if you use DataLoading if you want to check for some null ItemsSource. This event will be raised for child grids as well - in this case sender (which is GridViewDataControl) ParentRow property will be assigned.
Regards,Vlad
the Telerik team

This is my row loaded event
void rgvModifiedResults_RowLoaded(object sender, RowLoadedEventArgs e)
{
var row = e.Row as GridViewRow;
if (row != null)
{
row.Cells[7].AddHandler(
FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(GradingMeeting_MouseLeftButtonDown), true);
row.Cells[8].AddHandler(
FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(GradingMeeting_MouseLeftButtonDown), true);
row.Cells[9].AddHandler(
FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(GradingMeeting_MouseLeftButtonDown), true);
}
}
This is the event handler
void GradingMeeting_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
GridViewCell grdCell = (GridViewCell)sender;
GridViewRow grdRow = (GridViewRow)grdCell.ParentRow;
PerformResult performResult = null;
IOfflineAction act = null;
OriginalResults originalResults = (OriginalResults)grdRow.Item;
OfflineResults origRsults = (OfflineResults)grdRow.Item;
try
{
RadGridView childGrid = grdRow.ChildrenOfType<RadGridView>().FirstOrDefault();
if (grdRow.IsSelected && grdRow.HasHierarchy)
{
originalResults.SelectedItem = true;
if (childGrid.SelectedItems.Count == 1)
{
for (int i = 0; i < originalResults.ModifiedResults.Count; i++)
{
for (int x = 0; x < childGrid.SelectedItems.Count; x++)
{
if (originalResults.ModifiedResults[i].ConfirmationNumber.ToString().Equals(childGrid.SelectedItems[x].ToString()))
{
originalResults.ModifiedResults[i].SelectedItem = true;
}
}
}
}
else
{
MessageBox.Show("Compare feature can be used only if both the parent and child rows are selected. Please select a parent and child and try again. ");
}
}
else
{
if (childGrid.SelectedItems.Count == 2) (-- this is the line it throws an error because nothing is selected in the child grid)
{
for (int i = 0; i < originalResults.ModifiedResults.Count; i++)
{
for (int x = 0; x < childGrid.SelectedItems.Count; x++)
{
if (originalResults.ModifiedResults[i].ConfirmationNumber.ToString().Equals(childGrid.SelectedItems[x].ToString()))
{
originalResults.ModifiedResults[i].SelectedItem = true;
}
}
}
}
else
{
MessageBox.Show("Compare feature can be used only if both the parent and child rows are selected. Please select a parent and child and try again. ");
}
}
}
catch (Exception ex)
{
}
}
The child grid is not null as in data(it is null because the user hasnt selected anything from the child grid's select box)
Please let me know if you need any additional information.
Thanks
Sathish.K
Well, you can place check for childGrid != null to eliminate the exception.
All the best,
Milan
the Telerik team