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:
Hoping someone could offer some insight into what I'm doing wrong!
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!