开发者

Add an item to combobox before binding data from the database

I had a combobox in a Windows For开发者_运维问答ms 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";
    }
}


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;


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();


 CBParent.Insert(0,"Please select Category")


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-->"));


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.


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜