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

Dynamic insert with custom data binding values

1 Answer 127 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Gois
Top achievements
Rank 1
Michael Gois asked on 11 May 2009, 08:19 AM
Hi
I have a grid control which uses a template for the editing/inserting. In my template, I have my own user control which has a property which is usually databound.
When inserting by pressing the insert command in the grid command item template, I look out for the InitInsertCommandName, and create some default values for the insert template, like so:

protected void radApprovers_ItemCommand(object source, GridCommandEventArgs e)
       {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                // Cancel the default operation
                e.Canceled = true;
                //Prepare an IDictionary with the predefined values
                System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                newValues["company_id"] = cmbCompaniesFilter.SelectedCompanyId;         
                //Insert the item and rebind
                e.Item.OwnerTableView.InsertItem(newValues);
            }
}

As you can see, I'm setting the default values for the column company_id. Here's the problem:
I want to put the grid into insert mode dynamically (which I'm doing through a page url request variable). I'm using the following code to do so:

System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
 newValues["account_id"] = SessionManager.GetAccountId(this);
newValues["company_id"] = cmbCompaniesFilter.SelectedCompanyId;
gridApprovers.MasterTableView.IsItemInserted = true;
gridApprovers.MasterTableView.InsertItem(newValues);

The problem is that when I call InsertItem(newValues) I get the following error:
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.". I have to set a value here, otherwise it tries to bind a DBNull to an integer property, which will raise a "Specified cast is not valid" error when it binds.

Can you please tell me how I can go about dynamically creating databinding values when dynamically setting the grid into Insert mode?
Thanks


1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 14 May 2009, 10:52 AM
Hi Michael Gois,

Could you provide your aspx and code behind (please, use the Code Formatter tool of the editor) since I was not able to reproduce the problem as evidenced by the small sample I am sending you.

Thank you.

Best Regards,
Tsvetoslav
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
Michael Gois
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or