Dynamic insert with custom data binding values

2 posts, 0 answers
  1. Michael Gois
    Michael Gois avatar
    17 posts
    Member since:
    Oct 2008

    Posted 11 May 2009 Link to this post

    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


  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 14 May 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top