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

How to disable items according to a condition in combo box?

6 Answers 1284 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Seda
Top achievements
Rank 1
Seda asked on 06 Jan 2014, 04:05 PM

 I'm developing an ASP.NET Web Application in C#.

The user is selecting some listed values from radcombobox. Those values are being saved in Database. When the same user login and select the combo box, already selected values should be visible but shouldn't be get selected. I need to disable the values... Is there any options to do it.

Note : I'm populating the combo-box items from a database table.

6 Answers, 1 is accepted

Sort by
0
Gaurab
Top achievements
Rank 1
answered on 06 Jan 2014, 08:54 PM
Hello Seda,

Could you clarify if you are creating a Windows application or an ASP.NET Web application?  This forum is for the ASP.NET RadComboBox.  If you are creating a Windows application, please post your question here.

Thanks,

Jon
0
Seda
Top achievements
Rank 1
answered on 06 Jan 2014, 09:00 PM
Hello Jon thanks for your  warning. I made the correction, Do you have any idea about the solution?
0
Accepted
Gaurab
Top achievements
Rank 1
answered on 06 Jan 2014, 09:55 PM
I'm assuming you're using CheckBoxes because you said the user is selecting more than one item.

I set up a sample project.  I just used an XML file for the datasource.

<?xml version="1.0" encoding="utf-8" ?>
<items>
 <Item Text="New York" Value="1" />
 <Item Text="Paris" Value="2" />
 <Item Text="London" Value="3" />
 <Item Text="Houston" Value="4" />
 <Item Text="Chicago" Value="5" />
 <Item Text="Atlanta" Value="6" />
</items>

Here is the XmlDataSource on the page:

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="Cities.xml"/>


Here is the RadComboBox on the page:
<telerik:RadComboBox id="RadComboBox1"
    runat="server"
    OnDataBound="RadComboBox1_OnDataBound"
    CheckBoxes="True"
    datasourceid="XmlDataSource1"
    datatextfield="Text"
    datavaluefield="Value"/>


Here is the RadComboBox1_OnDataBound event handler in the code behind:

protected void RadComboBox1_OnDataBound(object sender, EventArgs e)
{
    var citiesSelected = new List<string> {"2", "4"};
    var rcb = (RadComboBox) sender;
 
    foreach (RadComboBoxItem item in rcb.Items)
    {
        if (citiesSelected.Contains(item.Value))
        {
            item.Checked = true;
            item.Enabled = false;
        }
    }
}

So pretend that your user has already previously selected items with ID "2" and "4" (and this was stored in your database).  Then these items will show up checked but disabled.

I think this is what you are looking for.  Let me know if you still have questions or I assumed something different than what you were asking.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 07 Jan 2014, 04:39 AM
Hi 

Please have a look into the following sample code snippet to achieve your scenario.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource1"
    DataTextField="Cityname"  EmptyMessage="--select--">
</telerik:RadComboBox>
<telerik:RadButton ID="RadButton1" runat="server" Text="Add Checked Item" OnClick="RadButton1_Click">
</telerik:RadButton>

C#:
protected void RadButton1_Click(object sender, EventArgs e)
{
    String connstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connstring);
    SqlDataAdapter adapter = new SqlDataAdapter();
    conn.Open();
    foreach (RadComboBoxItem item in RadComboBox1.CheckedItems)
    {
        item.Enabled = false;
        SqlCommand cmd = new SqlCommand("insert into CountryTable (CountryName) values('" + item.Text + "')", conn);
        cmd.ExecuteNonQuery();
    }
    conn.Close();
        RadComboBox1.ClearCheckedItems();
}

Let me know if you have any concern.
Thanks,
Shinu.
0
Seda
Top achievements
Rank 1
answered on 07 Jan 2014, 09:20 AM
Thank you so much Jon,  the problem solved with your help

Seda
0
Seda
Top achievements
Rank 1
answered on 07 Jan 2014, 09:23 AM
Hi Shinu, thank you so much for your posting, the problem is solved 

Thanks
Seda
Tags
ComboBox
Asked by
Seda
Top achievements
Rank 1
Answers by
Gaurab
Top achievements
Rank 1
Seda
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or