How to add first element as "SELECT" in rad combo box

9 posts, 1 answers
  1. Swapnil
    Swapnil avatar
    30 posts
    Member since:
    Sep 2012

    Posted 24 Sep 2012 Link to this post

    How to add first element as "SELECT" in rad combo box?
    and i want to bound other database items followed by select
    How can i do this plz Help.
    Thanks
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Sep 2012 Link to this post

    Hi Swapnil,

    Try the following code to achieve your scenario.

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
      SqlDataAdapter adapter = new SqlDataAdapter("SELECT EmployeeID, FirstName FROM Employees", con);
      DataTable dt = new DataTable();
      adapter.Fill(dt);
      rcb_Users.DataTextField = "FirstName";
      rcb_Users.DataValueField = "FirstName";
      rcb_Users.DataSource = dt;
      rcb_Users.DataBind();
    }
    protected void rcb_Users_DataBound(object sender, EventArgs e)
    {
      var combo = (RadComboBox)sender;
      combo.Items.Insert(0, new RadComboBoxItem("Select", string.Empty));
    }

    Thanks,
    Princy.
  3. Swapnil
    Swapnil avatar
    30 posts
    Member since:
    Sep 2012

    Posted 24 Sep 2012 Link to this post

    Getting Error at the line:
    var combo = (RadComboBox)sender;

    Error
    :
     The type or namespace name 'RadComboBoxItem' could not be found (are you missing a using directive or an assembly reference?)   

    Thanks Princy.
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Sep 2012 Link to this post

    Hi Swapnil,

    Please make sure that you adding the reference Telerik.Web.UI as follows.

    C#:
    using Telerik.Web.UI;

    Thanks,
    Princy.
  5. Swapnil
    Swapnil avatar
    30 posts
    Member since:
    Sep 2012

    Posted 24 Sep 2012 Link to this post

    Yes i have that namespace as well as that dll in bin folder of project,
    I have added the element "Select" by using the property   EmptyMessage="Select" and its working
    just want to know is it correct?
    Thanks
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Sep 2012 Link to this post

    Hi Swapnil,

    You can use EmptyMessage="Select"  if you doesn't want select as the first item. And the text will go when the control get focused or any item is selected.

    Hope this helps.

    Thanks,
    Princy.

  7. Swapnil
    Swapnil avatar
    30 posts
    Member since:
    Sep 2012

    Posted 26 Sep 2012 Link to this post

    select remains at first position because i have added other from database using property AppendDataBoundItems="true" and works correctely,
    so can i use it further or it will create any problem.
    Thanks
  8. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Sep 2012 Link to this post

    Hi,

    If you add select as an empty message you don't need to set AppendDataBoundItems="true". The empty message will be shown as text when nothing is selected in the RadCombobox. Here Select is not a RadCombobox item. But it will not create any problem. And if you need Select as a RadCombobox item you can add it as follows.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" Width="220px" DataTextField="name" DataValueField="name" DataSourceID="SqlDataSource1" AppendDataBoundItems="true">
     <Items>
        <telerik:RadComboBoxItem Text="Select" />
     </Items>
    </telerik:RadComboBox>

    Hope this helps.

    Thanks,
    Princy.
  9. Swapnil
    Swapnil avatar
    30 posts
    Member since:
    Sep 2012

    Posted 26 Sep 2012 Link to this post

    Thanks for the valuable reply
    it helps me,
    Thanks
Back to Top