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

Adding an Item to a databound Combobox with multiple columns

2 Answers 430 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
SSirica asked on 16 Feb 2017, 05:54 PM

I have the following radcombobox setup:

<telerik:RadComboBox ID="cbChannel2" runat="server" DropDownWidth="200px" HighlightTemplatedItems="True" Width="200px" DataTextField="sle_chnl_cd" DataValueField="sle_chnl_id" AutoPostBack="True" AppendDataBoundItems="True">
    <HeaderTemplate>
        <table cellpadding="1" cellspacing="0" class="Main" width="190px">
            <tr>
                <td class="ComboHeader2" width="50px" style="text-align: left; visibility: visible;"><b>Code</b>
                </td>
                <td class="ComboHeader2" width="140px" style="text-align: left; visibility: visible;"><b>Description</b>
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table cellpadding="1" cellspacing="0" class="Main" width="190px">
            <tr>
                <td width="50px" style="text-align: left; visibility: visible;">
                    <%#DataBinder.Eval(Container.DataItem, "sle_chnl_cd")%>
                </td>
                <td width="140px" style="text-align: left; visibility: visible;">
                    <%#DataBinder.Eval(Container.DataItem, "sle_chnl_name")%>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</telerik:RadComboBox>

Sometimes I have to add an additional row after the combobox has already been DataBound.  I understand how to add a radcomboboxitem  what I don't understand is how to add one such that "sle_chnl_name" actually gets a value?  How do I add a row that's contained in a dataset without losing the data that's already been loaded? 

2 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 21 Feb 2017, 11:55 AM

Hi,

You can add the item to the data source, then .Rebind() the combo box. With this it will take the new data source and will also have the newly added item.

Another option is to do this client-side and use JavaScript to add items. You can star from the Client Templates RadComboBox demo.

Regards,

Marin Bratanov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
answered on 21 Feb 2017, 03:40 PM

Thanks, I figured it out.  I ended up going the route of binding the columns from the attributes.  For anyone else looking for the solution the ItemTemplate looks like this:

<ItemTemplate>
    <table cellpadding="1" cellspacing="0" class="Main" width="350px">
        <tr>
            <td width="100px" style="text-align: left;">
                <%# DataBinder.Eval(Container, "Attributes['Code']") %>
            </td>
            <td width="250px" style="text-align: left;">
                <%# DataBinder.Eval(Container, "Attributes['']") %>
            </td>
        </tr>
    </table>
</ItemTemplate>

 

and the VB code looks like:

    Private Sub cb_ItemDataBound(sender As Object, e As RadComboBoxItemEventArgs) Handles cb.ItemDataBound
        Dim dataItem As DataRowView = CType(e.Item.DataItem, DataRowView)
        e.Item.Attributes("Code") = dataItem("Code")
        e.Item.Attributes("Desc") = dataItem("Desc")
        e.Item.DataBind()
    End Sub
Normal binding:
                cb.DataSource = ds.Tables(0)
                cb.DataBind()
Adding extra rows after binding:
    Dim item As New RadComboBoxItem
    item.Attributes("Code") = sqlDS.Tables(0).Rows(0).Item("Code")
    item.Attributes("Desc") = sqlDS.Tables(0).Rows(0).Item("Desc")
    item.Value = sqlDS.Tables(0).Rows(0).Item("ID").ToString()
    item.Text = sqlDS.Tables(0).Rows(0).Item("Code").ToString()
    item.Selected = True
    item.DataBind()
    cb.Items.Add(item)

 

Hope that helps someone struggling for the solution.

 

Tags
ComboBox
Asked by
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Marin Bratanov
Telerik team
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
Share this question
or