DropdownList DataSource
You can bind the DropDownList in different ways by using List, Dictionary, Enum, DataSet DataTable
.
Main you have to consider three thing while binding the datasource of a dropdown.
- DataSource - Name of the dataset or datatable or your datasource
- DataValueField - These field will be hidden
- DataTextField - These field will be displayed on the dropdwon.
you can use following code to bind a dropdownlist to a datasource as a datatable
:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
SqlCommand cmd = new SqlCommand("Select * from tblQuiz", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
DropDownList1.DataTextField = "QUIZ_Name";
DropDownList1.DataValueField = "QUIZ_ID"
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
if you want to process on selection of dropdownlist, then you have to change AutoPostBack="true"
you can use SelectedIndexChanged
event to write your code.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strQUIZ_ID=DropDownList1.SelectedValue;
string strQUIZ_Name=DropDownList1.SelectedItem.Text;
// Your code..............
}
How do I set a DataSource to a DropDownList?
I solved my problem like this:
DataSet ds = SomeMethodToFillTheDataSet()
foreach(DataRow row in ds.tables[0].Rows)
{
ListItem item = new ListItem();
item.text = "fieldName"; e.g Name
item.value = "FieldName"; e.g ID
DropDOwnList.Items.Add(item);
}
ListMyClass as datasource for DropDownList?
For web based ASP.net
You need to specify the drop down lists datatextfield
and datavaluefield
properties.
MyDropDownList.DataSource = myList;
MyDropDownList.DataTextField="Name";
MyDropDownList.DataValueField="ID";
MyDropDownList.DataBind();
For win form
You need to specify the displaymember
/ valuemember
properties.
Just noticed this is a winform application try this:
MyDropDownList.DataSource = myList;
MyDropDownList.DisplayMember = "Name";
MyDropDownList.ValueMember = "ID";
Updating the DataSource of a DropDownList in ASP.NET's C#
So I eventually resorted to doing what I did not want to do in the hopes that there would be a "Best-practice" way to do it (and one hopefully already built-in into DataSources), but it seems that was not to be.
All it took was to re-assign the DataSource's parameters to it again and then assign the DataSource back to the DropDownList, as if I were creating a new one.
I created the following method, which I then called at the end of my Delete button's event.
protected void DropDownList_Reload()
{
MyDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
MyDataSource.SelectCommand = "SELECT * FROM [tblMyTable] WHERE [IsDeleted] <> 1";
cbxDropDownList.DataSourceID = "MyDataSource";
cbxDropDownList.DataTextField = "MyHeader";
cbxDropDownList.DataValueField = "MyHeader";
}
Dropdownlist Datasource and adding extra item from C#
It's most probably because you're adding the item before the DataBind()
. If you want to add an item with
ddlVisualTemplate.Items.Add()
then you have to do it after the dropdown is being bound.
If you look at http://msdn.microsoft.com/en-us/library/ms178472.aspx then DataBind is being done in PreRenderComplete. So you have to add the element in some event that occurs after PreRenderComplete.
Or you could do it on the ddlVisualTemplate.DataBound
event.
How to bind a drop-down control to a data source in ASP.NET
As your code works for retrieving Employees
Info. from database, you will retrieve the Departments
info. from your Departments table.
protected void bindDepartments()
{
SqlConnection strcon1 = new SqlConnection(strcon);
strcon1.Open();
string ADDStr = "SELECT DepartmentId,DepartmentName FROM Departments ";
SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);
adapter.Fill(table);
ddlDepartments.DataSource = table;
ddlDepartments.DataValueField = "DepartmentId"; //The Value of the DropDownList, to get it you should call ddlDepartments.SelectedValue;
ddlDepartments.DataTextField = "DepartmentName"; //The Name shown of the DropDownList.
ddlDepartments.DataBind();
}
DropDown datasource
use property
public string NAME {get; set;}
Related Topics
Equivalent of SQL Isnull in Linq
What Is the Use for Task.Fromresult<Tresult> in C#
How to Find Out If a File Exists in C#/.Net
Azure Ad B2C - Role Management
Server Execution Failed (Exception from Hresult: 0X80080005 (Co_E_Server_Exec_Failure))
Datetime Conversion from String C#
How to Find Fqdn of Local MAChine in C#/.Net
Linq Order by Null Column Where Order Is Ascending and Nulls Should Be Last
How to Get the Closest Number from a List<Int> with Linq
How to Make a Hyperlink Work in a Richtextbox
Graphics.Drawstring VS Textrenderer.Drawtextwhich Can Deliver Better Quality