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

How to set the RadComboBox with appropriate Checkboxes

1 Answer 342 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Fawad
Top achievements
Rank 1
Fawad asked on 22 Nov 2014, 10:19 AM
Hi,

I have used RadComboBox inside my DetailsView. I send the value of combobox as comma separated to the database as (1,2,3,4). When I want to retrieve the data (1,2,3,4) from the database using SqlDataSource on edit mode of DetailsView, how can I set or bind the "Checked" values and display them as checked back in the combobox.

Here is what i have done:

--> WebForm1.Aspx

<%----------------------- DATA SOURCE FOR DELIVERABLE -------------------------%>
 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ITSConnectionString %>"
            DeleteCommand="DELETE FROM [Deliverable] WHERE [DeliverableId] = @DeliverableId"
            InsertCommand="INSERT INTO [Deliverable] ([DeliverableTitle],[CCIds]) VALUES (@DeliverableTitle, @CCIds)"
            SelectCommand="SELECT * FROM [Deliverable] WHERE ([DeliverableId] = @DeliverableId)"
            UpdateCommand="UPDATE [Deliverable] SET [DeliverableTitle] = @DeliverableTitle, [CCIds] = @CCIds WHERE [DeliverableId] = @DeliverableId">
            <DeleteParameters>
                <asp:Parameter Name="DeliverableId" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="DeliverableId" Type="int32" />
                <asp:Parameter Name="DeliverableTitle" Type="String" />
                <asp:Parameter Name="CCIds" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="DeliverableId" Type="int32" />
                <asp:Parameter Name="DeliverableTitle" Type="String" />
                <asp:Parameter Name="CCIds" Type="String" />
            </UpdateParameters>
   <SelectParameters>
      <asp:QueryStringParameter Name="DeliverableId" QueryStringField="DeliverableId" Type="Int32" />
   </SelectParameters>
</asp:SqlDataSource>
 
<%----------------------- DATA SOURCE FOR COMBOBOX -------------------------%>
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ITSConnectionString %>" SelectCommand="SELECT [UserId], [DisplayName] FROM [Users]"></asp:SqlDataSource>
 
<%----------------------- THE DETAILS VIEW -------------------------%>
 
<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" AutoGenerateRows="False" DataKeyNames="DeliverableId" DataSourceID="SqlDataSource2" Height="50px" Width="125px" OnItemInserting="DetailsView1_ItemInserting" OnDataBinding="DetailsView1_DataBinding" OnDataBound="DetailsView1_DataBound">
            <Fields>
                <asp:BoundField DataField="DeliverableId" HeaderText="DeliverableId" InsertVisible="False" ReadOnly="True" SortExpression="DeliverableId" />
                <asp:BoundField DataField="DeliverableId" HeaderText="DeliverableId" SortExpression="DeliverableId" />
                <asp:BoundField DataField="DeliverableTitle" HeaderText="DeliverableTitle" SortExpression="DeliverableTitle"/>
                <asp:TemplateField>
                    <ItemTemplate>
                     <telerik:RadComboBox ID="CCIds" Text='<% #Bind("CCIds") %>' runat="server"
                         DataSourceID="SqlDataSource1" DataTextField="DisplayName"  DataValueField="UserId"
                         CheckBoxes="true"></telerik:RadComboBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
                <asp:ButtonField Text="Insert" CommandName="Insert" />
                <asp:ButtonField Text="Update" CommandName="Update" />
            </Fields>
        </asp:DetailsView>

--> WebForm1.Aspx.CS


public string GetCheckBoxValues(string RadComboBoxName)
        {
            RadComboBox rcb = (RadComboBox)DetailsView1.FindControl(RadComboBoxName);
            var collection = rcb.CheckedItems;
            StringBuilder sbValues = new StringBuilder();
            if (collection.Count != 0)
            {
                foreach (var item in collection)
                {
                    sbValues.Append(item.Value);
                    sbValues.Append(Delimiter);
                }
                if (sbValues.ToString().EndsWith(Delimiter))
                    sbValues.Remove(sbValues.Length - 1, 1);
            }
            return sbValues.ToString();
        }
         
        // Get the comma separate values and insert them into the DB.
        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            string CCIds = GetCheckBoxValues("CCIds");
            if (CCIds != null)
            {
                e.Values["CCIds"] = CCIds;
            }
         }


Now how can I get the comma separated values and bound them with the combobox so that it should check only those items.

1 Answer, 1 is accepted

Sort by
0
Fawad
Top achievements
Rank 1
answered on 22 Nov 2014, 12:35 PM
Please ignore this post since I somehow handled it myself. Just for the documentation purpose, below are the steps how I resolved this issue:

1) Added a hidden textbox under the itemField --> ItemTemplate and binded the values of ComboBox that comes from DB.

<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="CCIds_HIDDEN" runat="server" Text='<%# Bind("CCIds") %>' Width="0" Visible="false" />

2) On the code behind, below are the things I managed.

        protected void checkcomboxes(RadComboBox comboBox, string values)
        {
            foreach (RadComboBoxItem item in comboBox.Items.ToList())
            {
                if (values.Contains(item.Value))
                    item.Checked = true;
                else
                    item.Checked = false;
            }
        }
        void bindOnEditLoad(SqlDataSource ds)
        {
            DataView dview = (DataView)ds.Select(DataSourceSelectArguments.Empty);
            string value = (String)dview.Table.Rows[0]["CCIds"];

            var bf = (RadComboBox)DetailsView1.FindControl("CCIds");
            checkcomboxes(bf, value);
        }
        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
            {
                bindOnEditLoad(SqlDataSource2);
            }
        }
Tags
ComboBox
Asked by
Fawad
Top achievements
Rank 1
Answers by
Fawad
Top achievements
Rank 1
Share this question
or