求一个combobox 绑定函数及其调用的例子

bkkkkk 2009-08-14 11:30:26
求一个combobox 绑定函数及其调用的例子
...全文
86 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqiuzaihui 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bkkkkk 的回复:]
有没有select from 数据库那种的
[/Quote]
private void InitialCustomer()
{
DataTable dtData = null;
try
{
using (SqlConnection thisConnection = new SqlConnection(
@"Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
using (SqlDataAdapter thisAdapter = new SqlDataAdapter(
@"SELECT CustomerID, CompanyName FROM Customers", thisConnection))
{
dtData = new DataTable("Customer");
thisAdapter.Fill(dtData);
thisConnection.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
dtData = null;
}

combobox.DataSource = dtData;
combobox.DisplayMember = "CustomerID";
combobox.ValueMember = "CompanyName";
}

其实一楼已经给你做好了, 你只需将数据库中的指定表填入DataTable就行了.
dancingbit 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bkkkkk 的回复:]
有没有select from 数据库那种的
[/Quote]
没有区别,无非就是这个DataTable是在程序中创建的,访问数据库需要使用SqlDataAdapter来Fill一个。
bkkkkk 2009-08-14
  • 打赏
  • 举报
回复
有没有select from 数据库那种的
lendylixt 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lovvver 的回复:]
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Text");

DataRow dr;
for(int i = 0 ; i < 5; i++)
{
    dr = dt.NewRow();
    dr[0] = i+1;
    dr[1] = string.Format("text-{0}",i+1);
    dt.Rows.Add(dr);
}

combobox.DataSource = dt;
combobox.DisplayMember = "Text";
combobox.ValueMember = "Id";
[/Quote]

最后加上
combobox.databind();
lovvver 2009-08-14
  • 打赏
  • 举报
回复
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Text");

DataRow dr;
for(int i = 0 ; i < 5; i++)
{
dr = dt.NewRow();
dr[0] = i+1;
dr[1] = string.Format("text-{0}",i+1);
dt.Rows.Add(dr);
}

combobox.DataSource = dt;
combobox.DisplayMember = "Text";
combobox.ValueMember = "Id";
  • 打赏
  • 举报
回复
这个就跟GridView差不多的
dancingbit 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 c123728529 的回复:]
private void Init_Combox()
        {
            string str = "select KuNumber from tb_CheKu";
            OleDbDataReader dr = connection.GetReader(str);
            string tmp = "";
            while (dr.Read())
            {
                tmp = dr[0].ToString();
                comboBox1.Items.Add(tmp);
            }
            comboBox1.SelectedIndex = 0;
        } 
这个比较简单
[/Quote]
这个不是绑定,只是简单地使用数据库中的数据填充ComboBox。
高效匠人 2009-08-14
  • 打赏
  • 举报
回复
private void Init_Combox()
{
string str = "select KuNumber from tb_CheKu";
OleDbDataReader dr = connection.GetReader(str);
string tmp = "";
while (dr.Read())
{
tmp = dr[0].ToString();
comboBox1.Items.Add(tmp);
}
comboBox1.SelectedIndex = 0;
}
这个比较简单

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧