locked
RadGrid: dynamically building a grid and adding a hierarchy with declarative relations row? RRS feed

  • Question

  • User-169079317 posted

    I have searched the internet and can't find out what my final problem is. What I have is RadGrid and I am creating a sub-column that is a bound column. Now, when I select the icon to open the sub-column the relation field's bound column appearing but even though I am binding a List<> to it I get the message: No child records to display. Below is the code so if anyone can help with my final problem that would be great!

     

    public partial class _Default : System.Web.UI.Page   
    {  
        protected RadGrid grid;  
        protected static List<TestListItem> list = new List<TestListItem>();  
        protected static List<TestListItem2> list2 = new List<TestListItem2>();  
          
       override protected void OnInit(EventArgs e)  
       {  
           DefineGridStructure();  
           base.OnInit(e);  
       }  
       private void DefineGridStructure()  
       {  
           this.grid = new RadGrid();  
           this.grid.AutoGenerateColumns = false;  
           this.grid.NeedDataSource += new GridNeedDataSourceEventHandler(OnNeedDataSource);  
           this.grid.ItemDataBound += new GridItemEventHandler(grid_ItemDataBound);  
           this.grid.DetailTableDataBind += new GridDetailTableDataBindEventHandler(grid_DetailTableDataBind);  
           this.grid.AllowMultiRowEdit = true;  
           this.grid.AllowPaging = true;  
           this.grid.AllowSorting = true;  
           this.grid.Width = 700;  
             
            
     
           //other columns definitions ----------------------------  
           string gridEditColumnLinkName = "Action Column";  
           GridEditCommandColumn gridEditColumn = new GridEditCommandColumn();  
           gridEditColumn.ButtonType = GridButtonColumnType.LinkButton;  
           gridEditColumn.EditText = "Edit";  
           gridEditColumn.UpdateText = "Save";  
           gridEditColumn.CancelText = "Cancel";  
           gridEditColumn.UniqueName = "ActionButton";  
           gridEditColumn.HeaderText = gridEditColumnLinkName;  
           gridEditColumn.HeaderStyle.Width = 50;  
     
           string boundColumnName1 = "Column 1";  
           GridBoundColumn boundColumn1 = new GridBoundColumn();  
           boundColumn1.DataField = "A";  
           boundColumn1.UniqueName = "Column1";  
           boundColumn1.HeaderText = boundColumnName1;  
           boundColumn1.HeaderStyle.Width = 300;  
             
           string boundColumnName2 = "Column 2";  
           GridBoundColumn boundColumn2 = new GridBoundColumn();  
           boundColumn2.DataField = "B";  
           boundColumn2.UniqueName = "Column2";  
           boundColumn2.HeaderText = boundColumnName2;  
           boundColumn2.HeaderStyle.Width = 300;  
     
           string boundColumnName3 = "Column 3";  
           GridBoundColumn boundColumn3 = new GridBoundColumn();  
           boundColumn3.DataField = "C";  
           boundColumn3.UniqueName = "Column3";  
           boundColumn3.HeaderText = boundColumnName3;  
           boundColumn3.HeaderStyle.Width = 300;  
             
           string dropdownColumnName = "Dropdown Column2";  
           GridBoundColumn dropdownColumn = new GridBoundColumn();  
           dropdownColumn.UniqueName = "Column4";  
           dropdownColumn.DataField = "D";  
           dropdownColumn.HeaderText = dropdownColumnName;  
           dropdownColumn.HeaderStyle.Width = 300;  
             
           //Detail table - (II in hierarchy level)  
           GridTableView tableView = new GridTableView(this.grid);  
           tableView.Width = Unit.Percentage(100);  
     
           tableView.DataKeyNames = new string[1] { "A1" };  
     
           GridRelationFields relationFields = new GridRelationFields();  
           relationFields.MasterKeyField = "A";  
           relationFields.DetailKeyField = "A1";  
           tableView.ParentTableRelation.Add(relationFields);  
     
           grid.MasterTableView.DetailTables.Add(tableView);  
     
           //Add columns  
           GridBoundColumn boundColumnr = new GridBoundColumn();  
           boundColumnr.DataField = "A1";  
           boundColumnr.HeaderText = "A1";  
           tableView.Columns.Add(boundColumnr);  
     
           this.grid.MasterTableView.DataKeyNames = new string[] { "A" };  
           this.grid.MasterTableView.Columns.Add(gridEditColumn);  
           this.grid.MasterTableView.Columns.Add(boundColumn1);  
           this.grid.MasterTableView.Columns.Add(boundColumn2);  
           this.grid.MasterTableView.Columns.Add(boundColumn3);  
           this.grid.MasterTableView.Columns.Add(dropdownColumn);  
           this.grid.MasterTableView.EditMode = GridEditMode.InPlace;  
           grid.PageSize = 5;  
           grid.ClientSettings.Scrolling.ScrollHeight = 230;  
           grid.ClientSettings.Scrolling.ScrollWidth = 750;  
           grid.ClientSettings.EnableClientKeyValues = true;  
           this.PlaceHolder1.Controls.Add(grid);  
     
       }  
     
       void grid_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)  
       {  
               e.DetailTableView.DataSource = list2;  
     
        }  
     
     
     
       void grid_ItemDataBound(object sender, GridItemEventArgs e)  
       {  
           if (e.Item is GridDataItem)  
           {  
               GridDataItem item = e.Item as GridDataItem;  
     
               if (e.Item.IsInEditMode)  
               {  
         
               }  
           }  
       }  
             
       protected void Page_Load(object sender, EventArgs e)  
       {  
                    List<string> alist = new List<string>();  
                    list = new List<TestListItem>();  
                    list2 = new List<TestListItem2>();  
          
                    alist.Add("1");  
                    alist.Add("2");  
                    alist.Add("3");  
                    alist.Add("4");  
                    alist.Add("5");  
                    alist.Add("5");  
                    list.Add( new TestListItem( "ItemA 0""ItemB a""ItemC a""ItemD a", alist ));  
                    list.Add( new TestListItem( "ItemA 1""ItemB b""ItemC b""ItemD b", alist ));  
                    list.Add( new TestListItem( "ItemA 2""ItemB c""ItemC c""ItemD c", alist ));  
                    list.Add( new TestListItem( "ItemA 3""ItemB d""ItemC d""ItemD d", alist ));  
                    list.Add( new TestListItem( "ItemA 4""ItemB e""ItemC e""ItemD e", alist ));  
                    list.Add( new TestListItem( "ItemA 5""ItemB f""ItemC f""ItemD f", alist ));  
     
                    list2.Add (new TestListItem2("SubItem A"));  
                    list2.Add(new TestListItem2("SubItem B"));  
                    list2.Add(new TestListItem2("SubItem C"));  
                    list2.Add(new TestListItem2("SubItem D"));  
                    list2.Add(new TestListItem2("SubItem E"));  
                    list2.Add(new TestListItem2("SubItem F"));  
       }  
     
     
        void OnNeedDataSource(object source, GridNeedDataSourceEventArgs e)    
        {  
            if (!e.IsFromDetailTable)  
            {  
                grid.DataSource = list;  
            }  
            else 
            {  
                //grid.MasterTableView.DataSource = list2;  
            }  
        }   
          
          public class TestListItem  
        {  
            private string _a;  
            private string _b;  
            private string _c;  
            private string _d;  
            List<string> _aList;  
     
            public TestListItem( string a, string b, string c, string d,List<string> aList )  
            {  
                this._a = a;  
                this._b = b;  
                this._c = c;  
                this._d = d;  
                this._aList = aList;  
            }  
     
            public string A  
            {  
                get 
                {  
                    return this._a;   
                }  
                set 
                {  
                    this._a = value;  
                }  
            }  
              
              
     
            public string B  
            {  
                get 
                {  
                    return this._b;  
                }  
                  
                            set 
                {  
                    this._b = value;  
                }  
     
            }  
              
            public string C  
            {  
                get 
                {  
                    return this._c;  
                }  
                  
                            set 
                {  
                    this._c = value;  
                }  
     
            }  
            public string D  
            {  
                get 
                {  
                    return this._d;  
                }  
     
                set 
                {  
                    this._d = value;  
                }  
     
            }  
              
            public List<string> AList  
            {  
                get 
                {  
                    return this._aList;  
                }  
                set 
                {  
                    this._aList = value;  
                }  
            }  
         }  
     
          public class TestListItem2  
          {  
              private string _a1;  
     
              public TestListItem2(string a1)  
              {  
                  this._a1 = a1;  
              }  
     
              public string A1  
              {  
                  get 
                  {  
                      return this._a1;  
                  }  
                  set 
                  {  
                      this._a1 = value;  
                  }  
              }  
       
          }  

     

    Sunday, April 25, 2010 8:04 PM

Answers

All replies