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

Grid Batch Mode using ListBox as Editor

5 Answers 167 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 22 Oct 2013, 04:28 PM
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?

5 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 25 Oct 2013, 12:02 PM
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.
0
John
Top achievements
Rank 1
answered on 31 Oct 2013, 06:07 PM
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.
0
Angel Petrov
Telerik team
answered on 05 Nov 2013, 11:51 AM
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.
0
J
Top achievements
Rank 1
answered on 24 Mar 2014, 11:09 PM
Thank you, this solved my issue!
0
krishna
Top achievements
Rank 1
answered on 06 Oct 2015, 12:31 PM

Hi,

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

Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
John
Top achievements
Rank 1
J
Top achievements
Rank 1
krishna
Top achievements
Rank 1
Share this question
or