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

Late Binding Hierarchical Grid

2 Answers 124 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Doug
Top achievements
Rank 1
Doug asked on 15 Dec 2011, 10:54 AM
I'm using the RadGrid control in a CompositeControl; the control is built and data source assigned (essentially just a collection) in the CreateChildControls method.  The issue I'm having is that I need to change what the grid is bound to depending on some input from the user.  The problem I've found is that CreateChildControls is called before the page event methods are fired, therefore meaning that the properties which I'm using to determine what exactly data to use to bind haven't been set.  I've tried overriding the OnPreRender method, and then setting the DataSource property again, and calling DataBind on the grid, and this seems to work until I try and expand one of the detail grid areas, at which point no data is displayed at all.

The code I'm using is (similar) to the following:

public class TestGrid : CompositeControl
{
    private readonly SearchCriteria searchCriteria = new SearchCriteria();
 
    private RadGrid grid;
 
    /// <summary>
    /// Contains search criteria set from controls on the parent page.
    /// This is what drives exactly what is displayed in the grid, and
    /// only seems to be set just before PreRender.
    /// </summary>
    [Browsable(false)]
    public SearchCriteria SearchCriteria
    {
        get
        {
            return this.searchCriteria;
        }
    }
 
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        grid.DataSource = this.GetJobs();
        grid.DataBind();
    }
 
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);       
        this.EnsureChildControls();
    }
 
    protected override void CreateChildControls()
    {
        this.Controls.Clear();
 
        grid = new RadGrid
        {
            ID = "grid",
            DataSource = this.GetJobs(),
            AutoGenerateColumns = false,
            AllowPaging = true,
            ShowStatusBar = true,
            PageSize = 10,
            Height = this.Height,
            Width = this.Width
        };
 
        grid.ClientSettings.Scrolling.AllowScroll = true;
        grid.MasterTableView.DataKeyNames = new[] { "BatchId" };
 
        grid.Columns.Add(new GridBoundColumn { DataField = "PolicyNumber", HeaderText = "Policy Number" });
        grid.Columns.Add(new GridBoundColumn { DataField = "Surname", HeaderText = "Surname" });
 
        var detailTable = new GridTableView(grid) { DataKeyNames = new[] { "BatchId" }, DataSource = this.GetDocuments() };
        detailTable.ParentTableRelation.Add(new GridRelationFields { DetailKeyField = "BatchId", MasterKeyField = "BatchId" });
 
        detailTable.Columns.Add(new GridBoundColumn { DataField = "PolicyNumber", HeaderText = "Policy Number" });
        detailTable.Columns.Add(new GridBoundColumn { DataField = "DocumentType", HeaderText = "Document Type" });
 
        grid.MasterTableView.DetailTables.Add(detailTable);
        this.Controls.Add(grid);
    }
 
    private IEnumerable<Job> GetJobs()
    {
        // Snipped for brevity.
    }
 
    private IEnumerable<Document> GetDocuments()
    {
        // Snipped for brevity.
    }
}


Hoping someone could offer some insight into what I'm doing wrong!

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Dec 2011, 11:02 AM
Hello Doug,

The optimum method to bind the detail table view is using DetailTableDataBind event which fires depending on the HierarchyLoadMode. Check the following help documentation which explains the same.
Hierarchical data-binding using DetailTableDataBind event.

-Shinu.
0
Doug
Top achievements
Rank 1
answered on 15 Dec 2011, 11:29 AM
Thanks Shinu.

What is the correct way for programmatically data binding if you need to wait for page events (Button.OnClick etc) to fire and manipulate properties etc before you bind?  I've read elsewhere on this forum that calling DataBind directly was incorrect?  If I set the DataSource and then call DataBind in OnPreRender it seems to cause some weird effects when expanding the detail grid.
Tags
Grid
Asked by
Doug
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Doug
Top achievements
Rank 1
Share this question
or