Guys,
I want to be able to add and display a detail table based on the griditem in the master table. I have an image button column upon clicking which, in the onItemCommand event handler I check the griditem and am trying to add a detail table to the grid if necessary and assign a datasource but it is not working. Anyone have any ideas on how to make this work. Please note that on the page init I have no way knowing about the detail table view. Below is the code.
Thanks,
Pavan.
public partial class _Default : System.Web.UI.Page
{
IList<DataItem> DataSource;
private void BuildSource()
{
List<DataItem> DataItemList = new List<DataItem>();
for (int i = 0; i < 5; i++)
{
DataItem di = new DataItem();
di.CustomerID = i.ToString();
di.ContactName = "Customer_" + i;
DataItemList.Add(di);
}
DataSource = DataItemList;
}
private void DefineGridStructure()
{
RadGrid RadGrid1 = new RadGrid();
RadGrid1.ID = "RadGrid1";
RadGrid1.DataSource = DataSource;
RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
RadGrid1.Width = Unit.Percentage(98);
RadGrid1.PageSize = 3;
RadGrid1.AllowPaging = true;
RadGrid1.AllowSorting = true;
RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
RadGrid1.AutoGenerateColumns = false;
RadGrid1.ShowStatusBar = true;
RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
RadGrid1.MasterTableView.PageSize = 20;
//Add columns
GridButtonColumn buttonColumn;
buttonColumn = new GridButtonColumn();
buttonColumn.ButtonType = GridButtonColumnType.ImageButton;
buttonColumn.CommandName = "expand";
buttonColumn.HeaderText = "";
buttonColumn.ImageUrl = "expand.gif";
buttonColumn.UniqueName = "expand";
buttonColumn.Text = "expand";
RadGrid1.MasterTableView.Columns.Add(buttonColumn);
GridBoundColumn boundColumn;
boundColumn = new GridBoundColumn();
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn = new GridBoundColumn();
boundColumn.DataField = "ContactName";
boundColumn.HeaderText = "Contact Name";
RadGrid1.MasterTableView.Columns.Add(boundColumn);
form1.Controls.Add(RadGrid1);
}
protected void Page_Init(object source, System.EventArgs e)
{
BuildSource();
DefineGridStructure();
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "expand")
{
RadGrid grid = (RadGrid)form1.FindControl("RadGrid1");
GridTableView tableview1 = new GridTableView(grid);
tableview1.DataSource = DataSource;
GridBoundColumn boundColumn;
boundColumn = new GridBoundColumn();
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
tableview1.Columns.Add(boundColumn);
boundColumn = new GridBoundColumn();
boundColumn.DataField = "ContactName";
boundColumn.HeaderText = "Contact Name";
tableview1.Columns.Add(boundColumn);
grid.MasterTableView.DetailTables.Add(tableview1);
}
}
}
I want to be able to add and display a detail table based on the griditem in the master table. I have an image button column upon clicking which, in the onItemCommand event handler I check the griditem and am trying to add a detail table to the grid if necessary and assign a datasource but it is not working. Anyone have any ideas on how to make this work. Please note that on the page init I have no way knowing about the detail table view. Below is the code.
Thanks,
Pavan.
public partial class _Default : System.Web.UI.Page
{
IList<DataItem> DataSource;
private void BuildSource()
{
List<DataItem> DataItemList = new List<DataItem>();
for (int i = 0; i < 5; i++)
{
DataItem di = new DataItem();
di.CustomerID = i.ToString();
di.ContactName = "Customer_" + i;
DataItemList.Add(di);
}
DataSource = DataItemList;
}
private void DefineGridStructure()
{
RadGrid RadGrid1 = new RadGrid();
RadGrid1.ID = "RadGrid1";
RadGrid1.DataSource = DataSource;
RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
RadGrid1.Width = Unit.Percentage(98);
RadGrid1.PageSize = 3;
RadGrid1.AllowPaging = true;
RadGrid1.AllowSorting = true;
RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
RadGrid1.AutoGenerateColumns = false;
RadGrid1.ShowStatusBar = true;
RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
RadGrid1.MasterTableView.PageSize = 20;
//Add columns
GridButtonColumn buttonColumn;
buttonColumn = new GridButtonColumn();
buttonColumn.ButtonType = GridButtonColumnType.ImageButton;
buttonColumn.CommandName = "expand";
buttonColumn.HeaderText = "";
buttonColumn.ImageUrl = "expand.gif";
buttonColumn.UniqueName = "expand";
buttonColumn.Text = "expand";
RadGrid1.MasterTableView.Columns.Add(buttonColumn);
GridBoundColumn boundColumn;
boundColumn = new GridBoundColumn();
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn = new GridBoundColumn();
boundColumn.DataField = "ContactName";
boundColumn.HeaderText = "Contact Name";
RadGrid1.MasterTableView.Columns.Add(boundColumn);
form1.Controls.Add(RadGrid1);
}
protected void Page_Init(object source, System.EventArgs e)
{
BuildSource();
DefineGridStructure();
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "expand")
{
RadGrid grid = (RadGrid)form1.FindControl("RadGrid1");
GridTableView tableview1 = new GridTableView(grid);
tableview1.DataSource = DataSource;
GridBoundColumn boundColumn;
boundColumn = new GridBoundColumn();
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
tableview1.Columns.Add(boundColumn);
boundColumn = new GridBoundColumn();
boundColumn.DataField = "ContactName";
boundColumn.HeaderText = "Contact Name";
tableview1.Columns.Add(boundColumn);
grid.MasterTableView.DetailTables.Add(tableview1);
}
}
}