How to Set ComboBox text and value?

Posted by Krishnamanohar under C# on 10/8/2012 | Points: 10 | Views : 37987 | Status : [Member] | Replies : 5
Hi
How to Add Text And Vaues To ComboBox?
How To Access The Values From ComboBox?
Help me Urgent
Regards
KrishnaManohar




Responses

Posted by: Self-Innovator on: 10/8/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi, try this

Front End

protected void Page_Load(object sender, EventArgs e)

{
if (!Page.IsPostBack)
{
string Con = ConfigurationManager.ConnectionStrings["dbConn"].ToString();
SqlConnection cnn = new SqlConnection(Con);
SqlCommand cmd = new SqlCommand("select * from tbl_Sample", cnn);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataValueField = "id";
DropDownList1.DataTextField = "numbers";
DropDownList1.DataBind();

}
}


Back End

create table tbl_Sample2

(
id int identity(1,1) not null,
numbers varchar(30)
primary key(id)
)


Add som evalues
insert into tbl_Sample values('ONE')

insert into tbl_Sample values('TWO')
insert into tbl_Sample values('THREE')


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 10/8/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this,
 

this.ComboBox1.DataSource = YourDataSet; // Set Data Source here
this.ComboBox1.DisplayMember ="Emp_Name"; // Column_Name.
this.ComboBox1.ValueMember = "Emp_Code"; // Column_Name

OR

ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;
comboBox1.Items.Add(item);

Access the value from combobox

string sText = comboBox1.SelectedItem;
string sValue = comboBox1.SelectedValue;

Refer this url

http://www.codeproject.com/Articles/15857/Add-Item-to-Binded-Combo-Box

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 10/10/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Hi.. @Krishnamanohar
if this helps u please Mark As Answer.

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pgayath on: 10/12/2012 [Member] Starter | Points: 25

Up
0
Down
Both should work for you.. Please mark answer

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Eldoraman on: 10/31/2014 [Member] Starter | Points: 25

Up
0
Down
Use a Dictionary Object and solve this problem

Dictionary comboSource = new Dictionary();
comboSource.Add("1", "Sunday");
comboSource.Add("2", "Monday");
comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

Full source..

http://net-informations.com/q/faq/combovalue.html

Eldo


Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response