Dear Supporter,
I've just started using Telerik, I could achieve a few things very nicely, but now I've run into a strange problem.
I'm trying to bind a grid to a hierarchical structure of business objects. I've set up the NeedDataSource event handler and even the DetailTableDataBind, and with the HierarchyDefaultExpanded = true parameter the hierarchy displays in the grid just as I need, the problem comes when the detail tables get collapsed and expanded again.
The code behind for a simple RadGrid (with 'RadGrid1' name):
When I collapse a detail table (by clicking on the expand/collapse button at the left side of the master row) and when I try to expand that again, the DetailsTableDataBind event handler throws an exception, because it doesn't receive the real business object (of the master row) which would contain the needed Contacts child list (the dataItem.DataItem in RadGrid1_DetailTableDataBind event handler is null after the collapse+expand action, but at first render it contains the needed Customer object as needed).
I have no more idea about the issue and I searched over the forum and googled a lot, but I always find solutions which binds to dataset or sqldatasources, but in our case custom classes is the only option.
I don't think I'm doing everything badly because the grid in full expanded mode displays correctly, only after collapse+expand the child table disappears (actually I think the master row's datasource is lost somewhere).
The pasted sample is a simplified version of our current situation, but it reflects my problem.
Btw. I was using RadControls for ASP.NET AJAX Q2 2012 SP2, Bin40 assemblies, 2012.2.912.40 versions.
Any help would be appreciated.
Regards,
Dexx
I've just started using Telerik, I could achieve a few things very nicely, but now I've run into a strange problem.
I'm trying to bind a grid to a hierarchical structure of business objects. I've set up the NeedDataSource event handler and even the DetailTableDataBind, and with the HierarchyDefaultExpanded = true parameter the hierarchy displays in the grid just as I need, the problem comes when the detail tables get collapsed and expanded again.
The code behind for a simple RadGrid (with 'RadGrid1' name):
public partial class Default : System.Web.UI.Page
{
public class Customer
{
public int id { get; set; }
public string name { get; set; }
public string city { get; set; }
public List<
Contact
> Contacts { get; set; }
}
public class Contact
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
}
public static List<
Customer
> Customers = new List<
Customer
> {
new Customer() { id = 1, name = "Bridget", city = "New York", Contacts = new List<
Contact
>() {
new Contact() { id = 1, name = "John", email = "johnny@hotmail.com" },
new Contact() { id = 2, name = "Mark", email = "mark@hotmail.com" }
}
},
new Customer() { id = 2, name = "Carol", city = "London", Contacts = new List<
Contact
>() {
new Contact() { id = 1, name = "Henry", email = "henry@hotmail.com" },
new Contact() { id = 2, name = "Hugo", email = "hugo@hotmail.com" },
}
}
};
void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
e.DetailTableView.DataSource = ((Customer)dataItem.DataItem).Contacts;
}
void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
if (!e.IsFromDetailTable)
RadGrid1.DataSource = Customers;
}
protected void Page_Init(object sender, EventArgs e)
{
RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
RadGrid1.DetailTableDataBind += RadGrid1_DetailTableDataBind;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
RadGrid1.MasterTableView.AutoGenerateColumns = true;
RadGrid1.MasterTableView.DataKeyNames = new string[] { "id" };
GridTableView contactsTable = new GridTableView(RadGrid1);
contactsTable.AutoGenerateColumns = true;
RadGrid1.MasterTableView.DetailTables.Add(contactsTable);
}
}
}
When I collapse a detail table (by clicking on the expand/collapse button at the left side of the master row) and when I try to expand that again, the DetailsTableDataBind event handler throws an exception, because it doesn't receive the real business object (of the master row) which would contain the needed Contacts child list (the dataItem.DataItem in RadGrid1_DetailTableDataBind event handler is null after the collapse+expand action, but at first render it contains the needed Customer object as needed).
I have no more idea about the issue and I searched over the forum and googled a lot, but I always find solutions which binds to dataset or sqldatasources, but in our case custom classes is the only option.
I don't think I'm doing everything badly because the grid in full expanded mode displays correctly, only after collapse+expand the child table disappears (actually I think the master row's datasource is lost somewhere).
The pasted sample is a simplified version of our current situation, but it reflects my problem.
Btw. I was using RadControls for ASP.NET AJAX Q2 2012 SP2, Bin40 assemblies, 2012.2.912.40 versions.
Any help would be appreciated.
Regards,
Dexx