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

How to add the first item

1 Answer 50 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 21 Oct 2014, 09:10 AM
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;

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Oct 2014, 02:31 PM
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.

 
Tags
DropDownList
Asked by
Felice
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or