1.combobox绑定数据库
在面页加载事件中绑定:
SqlConnection connection = new SqlConnection(connectionString)
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
this.combobox1.DataSource = ds.Tables[0];
this.combobox1.ValueMember = "字段名";//下拉框绑定的值,一般与displaymember一一对应;
this.combobox1.DisplayMember = "字段名"; //下拉框中显示的值。
this.combobox1.SelectedValue.ToString();//这是下拉框中的值被选定后,获得的被选项。
combobox1.DropDownStyle = ComboBoxStyle.DropDownList;//限制combobox的内容不能被用户编辑,只能从下拉表中选
2.datagridview绑定数据库
SqlConnection connection &#