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

RadGrid Edit Template with Multi-Select ListBox

4 Answers 312 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aaron Ford
Top achievements
Rank 2
Aaron Ford asked on 29 Mar 2011, 04:21 PM
I am using the current version of the RadGrid control for ASP.NET/AJAX and am following the edit template example using a SQLDataSource to handle Select/Insert/Update/Delete. I have the example working and would like to be able to have a multi-selectable listbox control, either the stabndard control or Rad control, that will populate the selected values into a single column within the RadGrid as a comma or semicolan delimited value. I would also like the item(s) of the list box to be selected based on the delimited value during edit events on existing records. Based on what I have coded, only the first selected item is inserted/updated into the corresponding RadGrid column and database. Do I need to add on_Updating and on_Inserting events in the code behind to handle this and how do I go about passing the correct value to the SQLDataSource?

The listbox contained within the edit template is as follows:

 

 

<asp:ListBox ID="lbLights" runat="server" SelectionMode="Multiple" SelectedValue='<%# Bind("Lights") %>'

 

 

 

DataSource='<%# (new string[] { "HIRL", "MIRL", "LIRL", "Centerline Lights", "Pilot Controlled", "Non-Standard", "None" }) %>' TabIndex="8" AppendDataBoundItems="True">

 

 

 

<asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>

 

 

 

</asp:ListBox>

 


Thanks,

Aaron

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Mar 2011, 09:20 AM
Hello Aaron,

You can find the SelectedValue from code behind(inside Update/Insert command) and add the corresponding insert parameter from code behind. Sample code is given below.

C#:
protected void RadGridDocuments_InsertCommand(object sender, GridCommandEventArgs e)
   {
       GridEditFormInsertItem insertitem = (GridEditFormInsertItem)e.Item;
       ListBox box = (ListBox)insertitem.FindControl("lbLights");
      int[] index = box.GetSelectedIndices();
      int i;
      string selectedvalue = "";
       foreach (object i_loopVariable in index)
       {
           i =Convert.ToInt32(i_loopVariable);
           selectedvalue = selectedvalue + box.Items[i].Value+',';//getting Selected values
 
       }
       SqlDataSource1.InsertParameters.Add("Lights", selectedvalue);//adding insert parameter
   }

Thanks,
Princy.
0
Aaron Ford
Top achievements
Rank 2
answered on 06 Apr 2011, 06:25 AM

Princy,

Thanks for the push in the right direction. I have that working for insert and the values are going in to the database column as a comma delimited set of values as needed. The issue I am having now is editing records that have been inserted and having each of the chosen values selected in the listbox. Where do I loop out the values from the comma delimited string and select the corresponding values in the listbox?

Thanks again,

Aaron

0
Iana Tsolova
Telerik team
answered on 08 Apr 2011, 04:00 PM
Hello Aaron,

You can try handling the ItemDataBound event for your purpose. There you can easily find the ListBox control and using the e.Item.DataItem property, you can get the string indicating which items in the ListBox should be selected.

For more information on accessing controls in RadGrid, check this help topic.

Regards,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Velmurugan
Top achievements
Rank 1
answered on 10 Nov 2011, 06:09 AM

hi..tsolova,
                i am using that code for inserting command.but i need how to use when updating command,already selected items displayed in listbox

protected void RadGridDocuments_InsertCommand(object sender, GridCommandEventArgs e)
   {
       GridEditFormInsertItem insertitem = (GridEditFormInsertItem)e.Item;
       ListBox box = (ListBox)insertitem.FindControl("lbLights");
      int[] index = box.GetSelectedIndices();
      int i;
      string selectedvalue = "";
       foreach (object i_loopVariable in index)
       {
           i =Convert.ToInt32(i_loopVariable);
           selectedvalue = selectedvalue + box.Items[i].Value+',';//getting Selected values
 
       }
       SqlDataSource1.InsertParameters.Add("Lights", selectedvalue);//adding insert parameter
   }

help me,,,,,
urgently need for my projects.........
Tags
Grid
Asked by
Aaron Ford
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Aaron Ford
Top achievements
Rank 2
Iana Tsolova
Telerik team
Velmurugan
Top achievements
Rank 1
Share this question
or