How to add the first item

2 posts, 0 answers
  1. Felice
    Felice avatar
    198 posts
    Member since:
    Aug 2013

    Posted 21 Oct 2014 Link to this post

    I fill this dropdown list in this way

    public  void FillDropDownList(string connString)
            {
                String Query = "SELECT * FROM information_schema.tables where Table_Name like 'Table%'";
                using (var cn = new SqlConnection(connString))
                {
                    cn.Open();
                    DataTable dt = new DataTable();
                    try
                    {
                        SqlCommand cmd = new SqlCommand(Query, cn);
                        SqlDataReader myReader = cmd.ExecuteReader();
                        dt.Load(myReader);
                    }
                    catch (SqlException e)
                    {
                       //TODO
                    }
                    radDropDownList1.DataSource = dt;
                    radDropDownList1.ValueMember = "TABLE_NAME";
                    radDropDownList1.DisplayMember = "TABLE_NAME";
                }
            }

    Now I need to add at index 0 something like "Select a table..."

    How do I do that?
    I have tried something like this but I need to add it at index 0.
    radDropDownList1.Items.Add("Select a table...");
    radDropDownList1.SelectedIndex = 0;
  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 23 Oct 2014 Link to this post

    Hello Felice,

    Thank you for writing.

    You are currently using the RadDropDownList in bound mode. However, you are trying to insert items in the RadDropDownList.Items collection which is relevant for unbound mode. For your specific case, you can either insert the default "Select a table..." item directly to your data source, or use the DropDownListElement.EditableElement.NullText property to specify the desired text when no item is selected:
    private void Form1_Load(object sender, EventArgs e)
    {
        this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
     
        this.radDropDownList1.DataSource = this.customersBindingSource;
        this.radDropDownList1.DisplayMember = "ContactName";
        this.radDropDownList1.ValueMember = "CustomerID";
     
        this.radDropDownList1.SelectedItem = null;
        this.radDropDownList1.DropDownListElement.EditableElement.NullText = "Select a customer";
        this.radDropDownList1.GotFocus += radDropDownList1_GotFocus;
        this.radDropDownList1.SelectedValueChanged += radDropDownList1_SelectedValueChanged;
    }
     
    private void radDropDownList1_SelectedValueChanged(object sender, EventArgs e)
    {
        SetForeColor();
    }
     
    private void radDropDownList1_GotFocus(object sender, EventArgs e)
    {
        SetForeColor();
    }
     
    private void SetForeColor()
    {
        if (this.radDropDownList1.SelectedItem == null)
        {
            this.radDropDownList1.DropDownListElement.EditableElement.Text = this.radDropDownList1.DropDownListElement.EditableElement.NullText;
            this.radDropDownList1.DropDownListElement.EditableElement.ForeColor = Color.Gray;
        }
        else
        {
            this.radDropDownList1.DropDownListElement.EditableElement.ForeColor = Color.Black;
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top