Grid Batch Mode using ListBox as Editor

6 posts, 0 answers
  1. John
    John avatar
    5 posts
    Member since:
    Jan 2012

    Posted 22 Oct 2013 Link to this post

    I have a grid using the Batch edit mode.  I have a need to use a multi-select ListBox as the editor for a particular column and to bind it using data from the server (in code-behind, not a front-end SQLDataSource, etc) and then pre-select items if they match information coming from the database.  How?  In previous grids, I could use ItemDataBound to preload the ListBox.  However, batch mode was a lot of work building client side code to handle multiple edits.  I'm wanting to use your new batch mode, but...

    I have tried to read/understand and follow the information here and here.

    I have tried all manner of the following in the PreRender method, but cannot get a hold of the listbox...
    <telerik:GridTemplateColumn DataField="MyDataField" HeaderText="My Header" UniqueName="MyUniqueName">
        <ItemTemplate>
            <%# Eval("MyBoundItem") %>
        </ItemTemplate>
        <EditItemTemplate>
            <telerik:RadListBox ID="MyRadListBox" runat="server" SelectionMode="Multiple" />
        </EditItemTemplate>
    </telerik:GridTemplateColumn>

    GridTableView masterTable = (sender as RadGrid).MasterTableView;
    RadListBox myRadListBox = masterTable.GetBatchColumnEditor("MyRadListBox") as RadListBox; // Suspicious Bind & null being returned
    myRadListBox.DataSource = myDataSource;
    myRadListBox.DataBind();

    How do I go about getting a hold of the ListBox, populate it from code-behind, and select the items in it based on values from the database?  Is this possible?
  2. Angel Petrov
    Admin
    Angel Petrov avatar
    1082 posts

    Posted 25 Oct 2013 Link to this post

    Hi John,

    In order to obtain a reference to the RadListBox in the PreRender event of the grid you can try the following:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            RadListBox listBox = (RadGrid1.MasterTableView.GetBatchColumnEditor("MyUniqueName") as GridTemplateColumnEditor).ContainerControl.FindControl("MyRadListBox") as RadListBox;
        }
    or
    protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            RadListBox listBox = (RadGrid1.MasterTableView.GetBatchEditorContainer("MyUniqueName") as Panel).FindControl("MyRadListBox") as RadListBox;
        }
    Both the GetBatchEditorContainer and GetBatchColumnEditor method receive as a parameter a column or it's unique name. That said calling them with the id of the editor will return null. If however you follow the approach from the code samples you should be able to make things work.

    One other thing which you should consider. In scenarios like the described a subscription to the four batch edit events is required(OnBatchEditGetCellValue,OnBatchEditSetCellValue,OnBatchEditGetEditorValue
     and OnBatchEditSetEditorValue). This is needed because there is no input element in the EdiItemTemplate and RadGrid has no way of knowing which value\values to display in the cell once edited. A explanation on how to handle these events is available in this help article.

    Regards,
    Angel Petrov
    Telerik
    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 the blog feed now.
  3. John
    John avatar
    5 posts
    Member since:
    Jan 2012

    Posted 31 Oct 2013 Link to this post

    Maybe there is an easier solution that I'm missing.  It doesn't have to be a listbox, just a multi-select control.  Is there an "easy" way to utilize a combo-box, maybe with checkboxes that allow multi-select?  Or some other multi-select control I could use?  Looking for more of a built in functionality rather than a lot of customizations our my part.
  4. Angel Petrov
    Admin
    Angel Petrov avatar
    1082 posts

    Posted 05 Nov 2013 Link to this post

    Hi John,

    Indeed using a combo with check boxes would facilitate you to use the built-in mechanisms of the grid. In attachments you can find a sample project which follows the scenario and demonstrates a possible realization. The main point in the example is to force the combo to display the selected values in it's input element. For that purpose you can set the CheckedItemsTexts property to DisplayAllInInput.

    Regards,
    Angel Petrov
    Telerik
    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 the blog feed now.
  5. J
    J avatar
    1 posts
    Member since:
    Mar 2014

    Posted 24 Mar 2014 in reply to Angel Petrov Link to this post

    Thank you, this solved my issue!
  6. krishna
    krishna avatar
    1 posts
    Member since:
    Nov 2011

    Posted 06 Oct 2015 Link to this post

    Hi,

      i am having the same scenario, but the radcombobox items will be binded from the code behind, so its not working for me

Back to Top