之前,从数据库绑定数据添加一个项目组合框 [英] add an item to combobox before bind data from data base

查看:204
本文介绍了之前,从数据库绑定数据添加一个项目组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在windows窗体从数据的基础上我这样做很好,但我想补充第一项< retrive数据组合框; - 请选择类别 - > befor从数据库中的数据我。如何能做到这一点?我可以把它

 公开类别()
{
的InitializeComponent()?;
CategoryParent();

}
私人无效CategoryParent()
{
$ B $使用B(的SqlConnection CON =的getConnection())
{

SqlDataAdapter的大=新SqlDataAdapter的(选择Category.Category,从类别Category.IdCON);
DataTable的DT =新的DataTable();
da.Fill(DT);
CBParent.DataSource = DT;
CBParent.DisplayMember =类别;
CBParent.ValueMember =ID;
}
}


解决方案

您可以将默认文本添加到组合框的像这样的文本属性(首选):

  CBParent.Text =<  - 请选择类别 - >中; 

或者,您可以添加值直接在数据表:

  da.Fill(DT); 
的DataRow行= dt.NewRow();
行[类别] =< - 请选择类别 - >中;
dt.Rows.InsertAt(行,0);
CBParent.DataSource = DT;


I had combobox in windows form which retrive data from data base I did this well but I want to add first item <-Please select Category-> befor the data from data base .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;

这篇关于之前,从数据库绑定数据添加一个项目组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆