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

Add Detailtable by Code

5 Answers 240 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Volker Weg
Top achievements
Rank 1
Volker Weg asked on 30 Apr 2009, 02:23 PM
Hello,

I try to create an DetailTable by Code.
My Code:

GridRelationFields grf = new GridRelationFields(); 
            grf.DetailKeyField = "ris_per_id"
            grf.MasterKeyField = "per_id"
 
            GridTableView gridRis = new GridTableView(RadGrid1); 
            gridRis.DataKeyNames = new string[] { "ris_id" }; 
            gridRis.Name = "tblRis"
            gridRis.ParentTableRelation.Add(grf); 
            gridRis.EnableViewState = true
            gridRis.HierarchyDefaultExpanded = true
             
 
            GridBoundColumn colDetRis = new GridBoundColumn(); 
            colDetRis.UniqueName = "ris_id"
            colDetRis.DataField = "ris_id"
            colDetRis.HeaderText = "ID"
            colDetRis.Visible = false
            colDetRis.ReadOnly = true
            gridRis.Columns.Add(colDetRis); 
 
            GridButtonColumn edit = new GridButtonColumn(); 
            edit.ButtonType = GridButtonColumnType.ImageButton; 
            edit.CommandArgument = "edit"
            edit.CommandName = "detailaction"
            edit.ImageUrl = "../images/edit.png"
            gridRis.Columns.Add(edit); 
 
            RadGrid1.MasterTableView.DataKeyNames = new string[] { "per_id" }; 
            RadGrid1.MasterTableView.DetailTables.Add(gridRis); 
            gridRis.Rebind(); 

Databinding:
protected void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e) 
        { 
            if (!e.IsFromDetailTable) 
                RadGrid1.DataSource = person.Data.DefaultView; 
 
            RadGrid1.MasterTableView.DetailTables[0].DataSource = ris.Data.DefaultView; // [0] = ris 
        } 

After Postback (any Postback - also any outside of the Grid) I get the following error:
[ArgumentOutOfRangeException: Der Index lag außerhalb des Bereichs. Er muss nicht negativ und kleiner als die Auflistung sein. Parametername: index] System.Collections.ArrayList.get_Item(Int32 index) +7483656 Telerik.WebControls.GridColumnCollection.System.Web.UI.IStateManager.LoadViewState(Object savedState) +199 Telerik.WebControls.GridTableView.LoadStructureState(Object SavedStructure) +229 Telerik.WebControls.RadGrid.LoadTableViewStructure(IEnumerator stateEnumerator, GridTableView tableView) +101 Telerik.WebControls.RadGrid.LoadTableViewStructure(IEnumerator stateEnumerator, GridTableView tableView) +358 Telerik.WebControls.RadGrid.LoadViewState(Object savedStateObject) +276 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +183 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Page.LoadAllState() +312 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1661

It sounds like the DetailTable looses its columns.
The DetailTable Creation I placed into the Page_Load (With or without PostBack).

Do I miss something to do?

Regards,

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 30 Apr 2009, 02:50 PM
Hello Volker,

Please examine the following links:

Documentation:
RadGrid programmatic creation

Online demos:
RadGrid programatic creation on PageInit
RadGrid programatic creation on PageLoad
Creating hierarchy programmatically

Let us know if you need further assistance.

Best Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Volker Weg
Top achievements
Rank 1
answered on 04 May 2009, 08:54 AM
Hi,

thanks for the Samples.
But now I have an another Problem.

Parent table relations set, but no correspoding data-key name found

I have tow tables:
tbl_per:
    Field: per_id
tbl_ris:
    Field: ris_id
    Field: ris_per_id

tbl_ris is one childtable of tbl_per. (1:n)

My Code:
 RadGrid1.MasterTableView.DataKeyNames = new string[] { "per_id" }; 
            GridRelationFields grf = new GridRelationFields(); 
            grf.DetailKeyField = "ris_per_id"
            grf.MasterKeyField = "per_id"
 
            GridTableView gridRis = new GridTableView(RadGrid1); 
            gridRis.DataKeyNames = new string[] { "ris_id" }; 
            gridRis.Name = "tblRis"
            gridRis.ParentTableRelation.Add(grf); 
            gridRis.EnableViewState = true
            gridRis.Width = Unit.Percentage(100); 
            RadGrid1.MasterTableView.DetailTables.Add(gridRis); 
 
            GridBoundColumn colDetRis = new GridBoundColumn(); 
            colDetRis.UniqueName = "ris_id"
            colDetRis.DataField = "ris_id"
            colDetRis.HeaderText = "ID"
            colDetRis.Visible = false
            colDetRis.ReadOnly = true
            gridRis.Columns.Add(colDetRis); 
 
            GridButtonColumn edit = new GridButtonColumn(); 
            edit.ButtonType = GridButtonColumnType.ImageButton; 
            edit.CommandArgument = "edit"
            edit.CommandName = "detailaction"
            edit.ImageUrl = "../images/edit.png"
            gridRis.Columns.Add(edit);             
- The Property "DataKeyNames hold the ID-Field of each table
- The GridRelationFields holds in the MasterKeyFile the ID-Field of tbl_per; DetailKeyField holds the ID-Field of the Detailtable
Still The Expand-event end in the above Error ...

Any Suggestion?

Regards,



0
Volker Weg
Top achievements
Rank 1
answered on 04 May 2009, 09:01 AM
Got it now.
I created the DetailTable on Page_load and not _init.
Now it works.

Thanks.

0
Volker Weg
Top achievements
Rank 1
answered on 05 May 2009, 07:49 AM
Sorry,
but It does not work correctly and I dont find out why.

Creating the Detailtable with the following code works basically, but I get after each postback more and more Columns with the Value "System.Data.DataRowView". Whitch usually tells me that I bound an Column not existing in the DataSource.

The Code looks like this:
/// <summary> 
        /// Creates the DetailTable
        /// </summary> 
        /// <param name="ContainerGrid">Grid</param> 
        /// <param name="MasterKeyField">ID-Field Mater</param> 
        /// <param name="DetailKeyField">ID-Field Detail</param> 
        /// <param name="TableName">Headline</param> 
        /// <param name="columns">columns Format: "datafieldname1;headertext1;dataformatstring1(def:"");visible1?(def:true);readonly1?(def:false)|datafieldname2 ... etc"</param> 
        public static void CreateDetailTableToGrid(RadGrid ContainerGrid, string MasterKeyField 
                                                                        , string DetailKeyField 
                                                                        , string TableName 
                                                                        , string columns) 
        { 
            ContainerGrid.MasterTableView.DataKeyNames = new string[] { MasterKeyField }; 
            GridRelationFields grf = new GridRelationFields(); 
            grf.DetailKeyField = DetailKeyField; 
            grf.MasterKeyField = MasterKeyField; 
 
            GridTableView gridDetail = new GridTableView(ContainerGrid); 
            //gridRis.DataKeyNames = new string[] { "ris_id" }; 
            gridDetail.Name = TableName
            gridDetail.ParentTableRelation.Add(grf); 
            gridDetail.EnableViewState = true
            gridDetail.NoDetailRecordsText = "Keine " + TableName + " vorhanden"; 
            gridDetail.Caption = TableName
            gridDetail.Width = Unit.Percentage(100); 
            ContainerGrid.MasterTableView.DetailTables.Add(gridDetail); 
 
            string[] cols = columns.Split('|'); 
            GridBoundColumn gCol = new GridBoundColumn(); 
 
            for (int i = 0; i < cols.Length; i++) 
            { 
                string[] properties = cols[i].Split(';'); 
                if (properties.Length < 2
                    throw new Exception("Columnconfiguration wrong. Missing Properties"); 
                gCol = new GridBoundColumn(); 
                gCol.UniqueName = properties[0].ToString(); 
                gCol.DataField = properties[0].ToString(); 
                gCol.HeaderText = properties[1].ToString(); 
                if (properties.Length > 2) 
                    gCol.DataFormatString = properties[2].ToString(); 
                if (properties.Length > 3) 
                    gCol.Visible = bool.Parse(properties[3].ToString()); 
                else 
                    gCol.Visible = true
                if (properties.Length > 4) 
                    gCol.ReadOnly = bool.Parse(properties[4].ToString()); 
                else 
                    gCol.ReadOnly = false
 
                gridDetail.Columns.Add(gCol); 
            } 
 
            GridButtonColumn edit = new GridButtonColumn(); 
            edit.ButtonType = GridButtonColumnType.ImageButton; 
            edit.CommandArgument = "edit" + TableName; 
            edit.UniqueName = "EditColumn" + TableName; 
            edit.CommandName = "detailaction"
            edit.ImageUrl = "../images/edit.png"
            gridDetail.Columns.Add(edit); 
 
            GridButtonColumn delete = new GridButtonColumn(); 
            delete.ButtonType = GridButtonColumnType.ImageButton; 
            delete.UniqueName = "DeleteColumn" + TableName; 
            delete.CommandArgument = "delete" + TableName; 
            delete.CommandName = "detailaction"
            delete.ImageUrl = "../images/edit.png"
            gridDetail.Columns.Add(delete);  
        } 

When I check the gridDetail.Columns it shows me the correct ammount of Collumns. Thouse collumns are also shown correctly.
As more DetailTables I expand as more wrong columns I get ...

Any Suggestions?

Regards,
0
Daniel
Telerik team
answered on 08 May 2009, 05:07 PM
Hello Volker,

In essence, there are two possible means to build Telerik RadGrid programmatically:
  • Create the control entirely in the code behind: This means that the grid instance should be created in the PageInit handler and added to a place holder's Controls collection at runtime, after the structure has been created. Note that in this case the grid columns should be added to the Controls collection of the MasterTableView after their attributes are set. No ViewState is required for grid structure to be persisted as it is recreated on each page initialization.
  • Create the control statically, but add its structure dynamically. This has to take place in the PageLoad event handler, following the rules for a structure object: columns or detail tables should be added to the corresponding collection first and then values for the properties of this instance should be set. Remember to check the condition (Not IsPostBack) to avoid adding the same structure objects to the grid twice. This is important because no ViewState is managed for the object before it has been added to the corresponding collection.
Note that hierarchical structure is not supported with simple data-binding (calling DataBind()). See the Simple data binding demo from the Populating with data section for more info about the limitations of this binding mode.

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Volker Weg
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Volker Weg
Top achievements
Rank 1
Share this question
or