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

Programmatically add rows to Details Table

3 Answers 490 Views
Grid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 27 Oct 2011, 05:48 PM

Anyone know how to programmatically add rows to a detail table in hierarchical radgrid?  I need to add rows to an unknown number of details tables at one time based on values the user has selected in a listbox. 

protected void btnAdd_Click(object sender, EventArgs e)
{
    foreach (RadListBoxItem li in lbSnSelected.Items)
    {
        foreach (GridDataItem gi in rgDefects.Items)
        {
            if ((gi.OwnerTableView.ParentItem as GridDataItem).GetDataKeyValue("SERIAL_NBR").ToString()
                == li.Value)
            {
                GridTableView tv = (GridTableView)gi.ChildItem.NestedTableViews[0];
 
                //Need to insert using Grid Item but not sure how to create and fill grid item object from
                //Details table
                //tv.InsertItem(ddTest.SelectedItem.Text, ddTest.SelectedValue, ddRefDes.SelectedValue,
                //    ddDefect.SelectedItem.Text, ddDefect.SelectedValue, rddTech.SelectedValue, DateTime.Now);
                //tv.Rebind();
            }
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 01 Nov 2011, 10:34 AM
Hello James,

 Each row in the TableView represents a record from the datasource that the grid is bound to. So in order to show more rows in the detail table you will need to first insert the data in the relevant datasource and then rebind the grid. You cannot construct dataItems manually and add them to the detail table. They are constructed internally by the grid when it is bound to data. The InsertItem method will open a new insert form in the grid, optionally prefilled with values that you have passed as parameters, but you can only show one such item per table view. Then you will have to hit the "Insert" button in order to save the new data to the database. The other option is not manually perform bulk inserts of the new data to the datasource and then rebind the grid which will refresh and show the newly insert items.

All the best,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Charles
Top achievements
Rank 1
answered on 20 Apr 2012, 07:17 PM
Thank you for the answer.  Although I am having trouble getting the values from the cells/textboxes that the details table displays when I select to Insert a new Item.  I have tried

 

 

 

GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;

string sWBS = insertedItem["WBS"].Text;

 

string sTaskName = insertedItem["TaskName"].Text;

But the values are an empty string?

0
Marin
Telerik team
answered on 21 Apr 2012, 08:18 AM
Hi Charles,

 The code snippet looks fine. If you call this in the InsertCommand event of the grid and you are using the default edit mode (EditForms) it should be working fine. If you are using InPlace edit mode you may need to cast the item to GridEditableItem or GridDataInsertItem. Another approach that you can try is the ExtractValuesFromItem as shown in this help topic:
http://www.telerik.com/help/aspnet-ajax/grid-inserting-values-inplace-and-editforms.html 

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
James
Top achievements
Rank 1
Answers by
Marin
Telerik team
Charles
Top achievements
Rank 1
Share this question
or