10

I had a combobox in a Windows Forms form which retrieves data from a database. I did this well, but I want to add first item <-Please select Category-> before the data from the database. How can I do that? And where can I put it?

public Category()
{
    InitializeComponent();
    CategoryParent();

}

private void CategoryParent()
{
    using (SqlConnection Con = GetConnection())
    {
        SqlDataAdapter da = new SqlDataAdapter("Select Category.Category, Category.Id from Category", Con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        CBParent.DataSource = dt;
        CBParent.DisplayMember = "Category";
        CBParent.ValueMember = "Id";
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Myworld
  • 1,861
  • 16
  • 45
  • 75

6 Answers6

16

You could either add the default text to the Text property of the combobox like this (preferred):

CBParent.Text = "<-Please select Category->";

Or, you could add the value to the datatable directly:

da.Fill(dt);
DataRow row = dt.NewRow();
row["Category"] = "<-Please select Category->";
dt.Rows.InsertAt(row, 0);
CBParent.DataSource = dt;
Forgotten Semicolon
  • 13,909
  • 2
  • 51
  • 61
2
public class ComboboxItem
{
    public object ID { get; set; }
    public string Name { get; set; }

}

public static List<ComboboxItem> getReligions()
{
    try
    {
        List<ComboboxItem> Ilist = new List<ComboboxItem>();
        var query = from c in service.Religions.ToList() select c;
        foreach (var q in query)
        {
            ComboboxItem item = new ComboboxItem();
            item.ID = q.Id;
            item.Name = q.Name;
            Ilist.Add(item);
        }
        ComboboxItem itemSelect = new ComboboxItem();
        itemSelect.ID = "0";
        itemSelect.Name = "<Select Religion>";
        Ilist.Insert(0, itemSelect);
        return Ilist;
    }
    catch (Exception ex)
    {
        return null;
    }    
}

ddlcombobox.datasourec = getReligions();
M Afifi
  • 4,645
  • 2
  • 28
  • 48
  • Good. Now, what's about if I use Anonymous type like e.g: **Select(x => new { Value = x.PROJECTGUID, Display = x.NAME + " - " + x.TFSPATH }** . Sample: `var listaProyectosDeSolucion = GestorDespliegues.SelectProyectos() .Distinct(comparer) .Where(x => x.TFSPATHSOLUTION == TFSPATHSOLUTION) .Select(x => new { Value = x.PROJECTGUID, Display = x.NAME + " - " + x.TFSPATH }).ToList(); cbProyectos.Load(listaProyectosDeSolucion, "Value", "Display");` – Kiquenet Oct 17 '14 at 11:31
0

You should add "Please select" after you bind data.

var query = from name in context.Version
                join service in context.Service 
                on name.ServiceId equals service.Id
                where name.VersionId == Id
                select new
                {
                    service.Name
                };

 ddlService.DataSource = query.ToList();
 ddlService.DataTextField = "Name";
 ddlService.DataBind();
 ddlService.Items.Insert(0, new ListItem("<--Please select-->"));
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
0
 CBParent.Insert(0,"Please select Category")
JoR
  • 295
  • 1
  • 4
  • 15
0

There are two quick approaches you could try (I don't have a compiler handy to test either one right now):

  1. Add the item to the DataTable before binding the data.
  2. You should be able to simply set CBParent.Text to "<- Please Select Category ->" after you bind the data. It should set the displayed text without messing with the items.
David
  • 208,112
  • 36
  • 198
  • 279
  • Dear David, you can not insert an item after you bind the data. The compiler gives this error: "Items collection cannot be modified when the DataSource property is set." Next time please consider having a handy compiler before answering a question that people will spend time on reading. – Dimitris Baltas Apr 21 '12 at 22:57
-1
void GetProvince()
{
    SqlConnection con = new SqlConnection(dl.cs);
    try
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT ProvinceID, ProvinceName FROM Province", con);
        DataTable dt = new DataTable();            
        int i = da.Fill(dt);

        if (i > 0)
        {           
            DataRow row = dt.NewRow();
            row["ProvinceName"] = "<-Selecione a Provincia->";
            dt.Rows.InsertAt(row, 0);

            cbbProvince.DataSource = dt;
            cbbProvince.DisplayMember = "ProvinceName";
            cbbProvince.ValueMember = "ProvinceID";
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
FAdao
  • 11
  • 1