[英]How to get Datasource() for Dropdownbox C# ASP.NET
I have table tblDepartments
with columns DeptID, Department, Description
. 我有表
tblDepartments
列DeptID, Department, Description
。 I also have dropdownbox drpDepartments
. 我也有dropdownbox
drpDepartments
。 I want to display the contents of column Department
on the dropdownbox. 我想在下拉框中显示“
Department
”列的内容。 I tried using this C# code for winforms but it didn't work: 我尝试对Winforms使用此C#代码,但没有成功:
drpDepartments.DataSource = dsDep.Tables["tblDepartment"];
drpDepartments.DisplayMember = "Department";
drpDepartments.ValueMember = "DeptID";
drpDepartments.Text = "Choose Department";
How do I do it using ASP.NET C#? 如何使用ASP.NET C#? Thanks.
谢谢。
New code (still not working) 新代码(仍然无法正常工作)
sConn = new SqlConnection(sStr);
daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
daDep = new SqlDataAdapter("SELECT * FROM tblDepartment", sConn);
dsEmp = new DataSet();
dsDep = new DataSet();
daEmp.Fill(dsEmp, "tblEmployee");
daDep.Fill(dsDep, "tblDepartment");
dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };
DataTable dt=new DataTable();
daDep.Fill(dt);
drpDepartments.DataTextField = "Department";
drpDepartments.DataValueField = "DeptID";
drpDepartments.DataSource = dt;
drpDepartments.DataBind();
You have to call it like this in asp.net webForms 您必须在asp.net webForms中这样称呼它
drpDepartments.DataSource = dsDep.Tables["tblDepartment"];// Set DataSource Table First
drpDepartments.DataTextField = "Department";// Set Column Name of DataTable to set as Text Field
drpDepartments.DataValueField = "DepartmentID";// Set Column Name of DataTable to set as Value Field
drpDepartments.DataBind();
You must be using it from using System.Web.UI.WebControls;
您必须
using System.Web.UI.WebControls;
使用它using System.Web.UI.WebControls;
namespace. 命名空间。 It seems like you were using winforms namespaces.
似乎您正在使用winforms命名空间。
DataTextField
is equivalent to DisplayMember
and ValueMember
is equivalent to DataValueField
. DataTextField
等效于DisplayMember
而ValueMember
等效于DataValueField
。
Did you call DataBind()? 您是否致电DataBind()?
drpDepartments.DataSource = dsDep.Tables["tblDepartment"];
drpDepartments.DisplayMember = "Department";
drpDepartments.ValueMember = "DepartmentID";
drpDepartments.Text = "Choose Department";
drpDepartments.DataBind();
drpDepartments.DataSource = ds1;
drpDepartments.DataTextField = "textcol";
drpDepartments.DataValueField = "valuecol";
drpDepartments.DataBind();
after doing this in btn_save method add this..
在btn_save方法中执行此操作后,添加此。
cmd.Parameters.AddWithValue("@a", drpDepartments.SelectedValue);
hope it helps.
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.