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

Details grid binding to the MasterTableView Datasource by default?

1 Answer 322 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 25 Apr 2012, 08:15 PM
I am building a Nested Detail grid programmatically in code behind only.
Sometimes the parent rows will have nested child data and sometimes they wont.

It appears that by default the details GridTableView is binding to the entire dataset of the MasterTable view (several hundred records).
I am using the "GridDetailTableDataBindEventHandler" for the detail grid and "GridNeedDataSourceEventHandler" for the MasterTable View.

Can anyone explain this to me?  Is it by design that the Nested Grid (I can see the datasource "count" as I step through the code - so I know what its doing) but it doesn't make any sense.

I did find something that (perhaps) states that this is by design - but its very convoluted and confusing:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/hierarchy/defaultcs.aspx 
"When RadGrid constructs each copy of the detail tables, it assigns its data-source of the grid itself. This is useful when the data source is a DataSet containing all the detail table data. Using the DetailTableDataBind() method, you can obtain the instance of the DataSet from the DataSource property of the detail table that is an argument of the event handler (referred to as e.DetailTable). Prior to rebinding MasterTableView or any detail tables, RadGrid fires NeedDataSource event, so the developer can assign a DataSource. Note that it will be fired only once for a group of detail tables and only if the DataSource of RadGrid is not already assigned."

My approach has been to pass 'dummy' values of zero to the datasource if there shouldn't be any nested data - very clunky and not what I was looking for...

my code behind (pertinent stuff only) is below:

// build the grids MasterTableView in OnInit (this sits in a SharePoint 2010 webpart)
                _radGridViewSearch = new Telerik.Web.UI.RadGrid();
                // advanced data filtering
                _radGridViewSearch.NeedDataSource += new GridNeedDataSourceEventHandler(radGridViewSearch_NeedDataSource);               
                _radGridViewSearch.Skin = "Sunset";
                _radGridViewSearch.AutoGenerateColumns = false;
                _radGridViewSearch.AllowPaging = true;
                _radGridViewSearch.AllowSorting = true;
                _radGridViewSearch.PagerStyle.Mode = GridPagerMode.Slider;
                _radGridViewSearch.PageSize = 25;
                _radGridViewSearch.AllowFilteringByColumn = true;
                // per telerik: Depending on the data source, the filtering may be case-sensitive or case-insensitive. You can control this behavior using the GroupingSettings-CaseSensitive property
                _radGridViewSearch.GroupingSettings.CaseSensitive = false;               
                // per telerik: In some .NET 3.5 scenarios you should also turn off the Linq expressions - EnableLinqExpressions="false"
                _radGridViewSearch.EnableLinqExpressions = false;
                _radGridViewSearch.MasterTableView.TableLayout = GridTableLayout.Auto;
 
// build the detail tableview - if the report needs one               
                if (_genReport.HasDetailsView)
                {
                     
                    // use 'ServerBind' so that the DetailTableDataBind event fires immediately after the corresponding parent item is bound                   
                    // Assign the DetailTableDataBind event handler to the grids nested matters              
                    _radGridViewSearch.DetailTableDataBind += new GridDetailTableDataBindEventHandler(_radGridViewSearch_DetailTableDataBind);
                    _radGridViewSearch.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerBind;                   
                    _detailsView = new GridTableView();
                    _detailsView.HierarchyLoadMode = GridChildLoadMode.ServerBind;
                    _detailsView.AllowPaging = false;
                    _detailsView.AllowSorting = false;
                    _detailsView.AllowFilteringByColumn = false;
                    _detailsView.Width = Unit.Percentage(95);                   
                    _detailsView.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;                   
                    _radGridViewSearch.MasterTableView.DetailTables.Add(_detailsView);                   
                     
                }
void _radGridViewSearch_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
        {
            try
            {
                // grab the 'parent item'
                GridDataItem targetParentItem = (GridDataItem)e.DetailTableView.ParentItem;
                int targetParentMatterID = 0;
                string targetChildMatter = "0";
                _lionRepository = new LionBCSRepository();
                // first, prep the parent matter ID and make sure we have something that we can work with
                if (!string.IsNullOrEmpty(Convert.ToString(targetParentItem["MatterID"].Text))
                    && (int.TryParse(targetParentItem["MatterID"].Text, out targetParentMatterID)))
                {
                    // next, prep the related matter ID and make sure we have something that we can work with                   
                    if (!string.IsNullOrEmpty(Convert.ToString(targetParentItem["RelatedMatter"].Text))
                        && !string.Equals(" ", Convert.ToString(targetParentItem["RelatedMatter"].Text), StringComparison.OrdinalIgnoreCase))
                    {
                        // once we're here we know that we have something to work with for both the parent and child matters                       
                        targetChildMatter = targetParentItem["RelatedMatter"].Text;
                        // setup the datasource                        
                        e.DetailTableView.DataSource = _lionRepository.GetRelatedMatters(targetChildMatter, targetParentMatterID);
                    }
                    else
                    {
                        e.DetailTableView.DataSource = _lionRepository.GetRelatedMatters(targetChildMatter, targetParentMatterID);
                    }
                }
                else
                {
                    e.DetailTableView.DataSource = _lionRepository.GetRelatedMatters(targetChildMatter, targetParentMatterID);
                }
            }
            catch (Exception ex)
            {
                LionExceptionLogging.LogException("WebParts", string.Format("LION SS exception at _radGridViewSearch_DetailTableDataBind: {0} Details: {1}", ex.Message, ex.InnerException));
                this._LitError.Visible = true;
                this._LitError.Text += ex.Message;
            }
        }

1 Answer, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 30 Apr 2012, 01:49 PM
Hello Martin,

It is design such way because you may actually want to show in the detail tables some data from the datasource of the grid itself. The grid's datasource could be DataSet that to contains several tables.
You may want to see this help topic and demos:
http://www.telerik.com/help/aspnet-ajax/grid-hierarchical-data-binding-using-detailtabledatabind.html
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/autogeneratedhierarchy/defaultcs.aspx

Kind regards,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Martin
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Share this question
or