This is a migrated thread and some comments may be shown as answers.

How to Check the child grid is null?

3 Answers 159 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sathish
Top achievements
Rank 1
Sathish asked on 19 Aug 2010, 10:53 PM

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

Sort by
0
Vlad
Telerik team
answered on 20 Aug 2010, 06:47 AM
Hi,

 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Sathish
Top achievements
Rank 1
answered on 20 Aug 2010, 02:26 PM
Hi Vlad,
    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
0
Milan
Telerik team
answered on 23 Aug 2010, 10:09 AM
Hi Sathish,

Well, you can place check for childGrid != null to eliminate the exception.


All the best,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Sathish
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Sathish
Top achievements
Rank 1
Milan
Telerik team
Share this question
or