DropDownList1.DataSource = 这里是查询出来的集合; //指定DropDownList使用的表里的那些字段 DropDownList1.DataTextField = "表里的Name字段"; //dropdownlist的Text的字段 DropDownList1.DataValueField = "这里是一般放表里的ID做为value值";//dropdownlist的Value的字段 DropDownList1.DataBind();
解决方案 »
- 请教一个多任务同时执行的问题
- 在C#中如何最快的加载图片到控件上
- Socket SocketFlags 标识符的意思
- 如何将信息存入文件中
- 图片的导入与导出,高手帮忙啊,分不多,以后补上
- C++里面的const函数在C#里面有相似的用法吗?
- c# 动态编译后内存释放问题
- 在.NET中有类似drawpixel的函数吗??
- 有关iis升级的问题
- winFrom 抓取屏幕指定区域 代码运行15分钟左右就出现(缓冲操作当前正在进行中,无法释放 BufferedGraphicsContext)
- wince C# Datagrid列宽设置
- 刚接触C#,现在要写一个串口程序,那位大侠给推荐一下快速入门的书籍
{
SqlDataAdapter sda = new SqlDataAdapter("select * from tb",conn);
DataSet ds = new DataSet();
sda.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "Name字段"; //dropdownlist的Text的字段
DropDownList1.DataValueField = "value值";
DropDownList1.DataBind();
}
{
SqlDataAdapter sda = new SqlDataAdapter("select name from name",conn);
DataSet ds = new DataSet();
sda.Fill(ds);for(int i=0;i<ds.Table[0].Rows.count;i++)
{
DropDownList1.Item.Add(ds.Table[0].Rows[i][0].tostring());
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace test
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
/// <summary>
/// 获取数据,返回dataset,用来绑定到下拉列表中
/// </summary>
public DataSet getData()
{
DataSet ds = null;
using (SqlConnection con = new SqlConnection("server=172.16.8.116;database=test;uid=sa;pwd=888888"))
{
string sql="select name from names";//name 是表 names中的字段
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (!string.IsNullOrEmpty(sql))
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds);
}
}
}
return ds;
} private void Form3_Load(object sender, EventArgs e)
{
this.comboBox1.DisplayMember = "name";//设置显示的是表中的哪一列
this.comboBox1.DataSource = getData().Tables[0];//绑定数据源
}
}
}
希望这些能够帮助你,这个是winform中的,web中的跟它原理一模一样,希望你能理解