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

Populating Listbox template in Grid ItemDataBound

2 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lenny_shp
Top achievements
Rank 2
Lenny_shp asked on 03 Dec 2008, 05:57 PM
I defined a listbox as GridTemplateColumn.  
It populates the available selections fine, but I could not get it to select with the value from database automatically without additional code in ItemDataBound.

Also am I defining DataField correctly in both template and listbox itself?

                <telerik:GridTemplateColumn UniqueName="colLstOpt" HeaderText="Rebalance" DataField="Opt_In" DataType="System.String" >
                     <ItemTemplate>
                            <asp:ListBox ID="lstOpt" runat="server" DataSourceID="XmlDataSource1" DataField="Opt_In" SelectionMode="Single" DataTextField="Name" DataValueField="Value"></asp:ListBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>      

Is it supposed to work with above code without having to code in ItemDataBound below?  

    Private Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If (TypeOf e.Item Is GridEditableItem AndAlso CType(e.Item, GridEditableItem).IsInEditMode) Then
            Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
            CType(editedItem.FindControl("lstOpt"), ListBox).SelectedValue = e.Item.DataItem("Opt_In").ToString
        End If
    End Sub

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Dec 2008, 07:11 AM
Hi Lenny,

You can set the SelectedValue property for the ListBox in the aspx to achieve the desired scenario. I tried it on my end and it is working correctly. Here is the code I tried.

ASPX:
<telerik:GridTemplateColumn UniqueName="TempCol" DataField="ProductName"  HeaderText="TempCol"   > 
                          <ItemTemplate> 
                              <asp:ListBox ID="ListBox1" runat="server" SelectedValue='<%#Eval("ProductName") %>' DataSourceID="SqlDataSource1" SelectionMode="Single" DataTextField="ProductName" DataValueField="ProductName"  ></asp:ListBox> 
                          </ItemTemplate> 
                        </telerik:GridTemplateColumn> 


Thanks
Shinu.
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 Dec 2008, 07:17 AM
Hi,

Try  setting the SelectedVaue of the ListBox as shown below:


   <telerik:GridTemplateColumn UniqueName="colLstOpt" HeaderText="Rebalance"  DataType="System.String" >
                     <ItemTemplate>
                        <asp:ListBox ID="lstOpt" runat="server" DataSourceID="AccessDataSource1"  DataField="EmployeeID" SelectionMode="Single" DataTextField="EmployeeID" DataValueField="EmployeeID"></asp:ListBox>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ListBox ID="lstOpt1" SelectedValue='<%#Eval("EmployeeID") %>' runat="server" DataSourceID="AccessDataSource1" DataField="EmployeeID" SelectionMode="Single" DataTextField="EmployeeID" DataValueField="EmployeeID"></asp:ListBox>

                    </EditItemTemplate>
                </telerik:GridTemplateColumn>

Thanks,
Princy
Tags
Grid
Asked by
Lenny_shp
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or