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

Adding new row to Rad Grid without rebind

1 Answer 349 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rameshkumar Gundalapudi Vijayakumar
Top achievements
Rank 1
Rameshkumar Gundalapudi Vijayakumar asked on 30 Nov 2010, 02:10 PM
We have Radgrid with Rad comboboxes kept in GridTemplateColumn Item Templates. Selecting items from existing rows comboboxes and then clicking on add button the grid got rebinded and selected options are not shown. When Add button is clicked we dont want to save new row in database but want to show new row in Radgrid, once required rows are added if we click another button "save" then we need to save the selected values from comboboxes from all rows to database. Can you please suggest the way to add new row to the Radgrid directly client side rather than binding the grid again for getting new row from database. And also suggest for retaining the combobox selected values on adding new row.

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 02 Dec 2010, 03:04 PM
Hi Rameshkumar,

This is expected behavior. RadGrid rebinds automatically on data-related commands, including InitInsert (the command fired when you open the insert form). Rebinding means grid items are recreated and any unsaved changes made in your items are gone.

To work around this behavior you can take the following action:

1. Make RadGrid always initialize the insert form on every postback:

protected void Page_Load(object sender, EventArgs e)
{
    RadGrid1.MasterTableView.IsItemInserted = true;
}

2. Use the ItemCreated event to hide the insert form on the client:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode)
    {
        e.Item.Style["display"] = "none";
    }
}

3. Use RadGrid's OnCommand event to show the insert form on the client when InitInsert is initiated and hide the same when CancelInsert command is fired:

<ClientSettings>
    <ClientEvents OnCommand="gridCommand" />
</ClientSettings>

function gridCommand(sender, args)
{
    if (args.get_commandName() == "InitInsert")
    {
        args.set_cancel(true);
        $telerik.$(".rgEditForm", sender.get_masterTableView().get_element()).parents("tr").show();
    }
 
    if (args.get_commandName() == "CancelInsert")
    {
        args.set_cancel(true);
        $telerik.$(".rgEditForm", sender.get_masterTableView().get_element()).parents("tr").hide();
    }
}

Attaching a test page to demonstrate this behavior.

Greetings,
Veli
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Rameshkumar Gundalapudi Vijayakumar
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or