access to property on selection winforms combobox - c#

One article has Name and Price properties. I use Name property to display articles inside combobox cmbDataList like this
public Form1()
{
InitializeComponent();
cmbDataList.DataSource = GetData();
cmbDataList.DisplayMember = "Name";
}
After user selected the preffered article I want to use it's Price property to assign to textbox on the same form. So, how to access to that Price property?
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
//var sel = cmbDataList.SelectedItem;
}

You have to cast SelectedItem to proper object.
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
var sel = (YourObject)cmbDataList.SelectedItem;
txt.Text = sel.Price.ToString();
}

Unless all names are unique, you're going to need a unique identifier to reference, for example an articleID.
From here, set the ComboBox's ValueMember like so;
cmbDataList.ValueMember = "ID";
then you can get your value on the event handler;
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
var sel = cmbDataList.SelectedValue;
//From here you're going to need to find your article with that particular ID.
}
Alternatively. You could have your DisplayMember as the article name, and the price as the ValueMember, then get it in the event handler for SelectedIndexChanged in the same way i put above. SelectedValue will then return the price;
cmbDataList.ValueMember = "Price";
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
var yourSelectedPrice = cmbDataList.SelectedValue;
}

Assuming GetData() returns a table, you need to write the ValueMember also... like this:
InitializeComponent();
cmbDataList.DataSource = GetData();
cmbDataList.DisplayMember = "Name";
cmbDataList.ValueMember = "Price";
Now, your selected display will be synced with the value and you will be able to use it..
Get more info in here:
Populate combobox

You Need to set ValueMember You can set in this way
cmbDataList.ValueMember = "ID";
then you write the code on cmbDataList_SelectedIndexChanged Event
May be this will help you
var sel = cmbDataList.SelectedValue

Related

How to get values from BindingSource in C#

I am working with WinForm in this i have 3 RadioButton one ComboBox and have three BindingSource
I want that when I check any of the RadioButtons, the values from the particular DataSources get bound to the ComboBox's ValueMember and DisplayMember.
The program has a SQL query where I want to send value based on the checked radio button.
private void rdbMed_CheckedChanged(object sender, EventArgs e)
{
cmbStock.DataSource = null;
cmbStock.DataSource = this.medicinesBindingSource;
this.cmbStock.DisplayMember = ""; //i want to bind "Med_ID" from medicinesBindingSource
this.cmbStock.ValueMember = ""; //"Med_Name"
}
private void rdbCat_CheckedChanged(object sender, EventArgs e)
{
cmbStock.DataSource = null;
cmbStock.DataSource = this.categoriesBindingSource;
this.cmbStock.DisplayMember = ""; //"category_Name"
this.cmbStock.ValueMember = ""; // category_ID"
}
private void rdbPharm_CheckedChanged(object sender, EventArgs e)
{
cmbStock.DataSource = null;
cmbStock.DataSource = this.pharmaceuticalBindingSource;
this.cmbStock.DisplayMember = "";// "Pharma_Name"
this.cmbStock.ValueMember = "";// "Pharma_ID"
}
Bellow are the parameter of the Query. It will help you to understand what I want to achieve.
if (rdbMed.Checked)
{
con.cmd.Parameters.AddWithValue("#Med_ID", cmbStock.ValueMember);
con.cmd.Parameters.AddWithValue("#category_ID", "");
con.cmd.Parameters.AddWithValue("#Pharma_ID", "");
}
else if (rdbCat.Checked)
{
con.cmd.Parameters.AddWithValue("#Med_ID", "");
con.cmd.Parameters.AddWithValue("#category_ID", cmbStock.ValueMember);
con.cmd.Parameters.AddWithValue("#Pharma_ID", "");
}
else if (rdbPharm.Checked)
{
con.cmd.Parameters.AddWithValue("#Med_ID", "");
con.cmd.Parameters.AddWithValue("#category_ID", "");
con.cmd.Parameters.AddWithValue("#Pharma_ID", cmbStock.ValueMember);
}
Easiest and the least pain way of doing this is resolving it inside SQL command and way to do it is cast your columns with AS VM and AS DM and make your DisplayMember = "DM" and ValueMember = "VM", so if you have sql query like SELECT USERID AS VM, NAME AS DM or SELECT PRODUCTID AS VM, NAME AS DM it will both work.
Problem later is if you do something with code and you may make mistakes trying to get USERID from databinding but it is instead VM. To avoid this you could "double select" values in query like SELECT USERID, NAME, ...., USERID AS VM, NAME AS DM this way you will have VM and DM for your controls but still hold original columns.

ComboBox behaving in a not expected way

I have two comboboxes which I populate using my GetAllCities() method in the CtrlMap.
My idea is, whenever I select another city on the ddFrom it should databind all the cities to ddTo (and later on remove the exact same selected, so user won't be able to select same city as point From and To).
However, Whenever I select something on ddFrom, ddTo populates (as it should), but SelectedIndex gets the same as the ddFrom. Same goes in the opposite way. If I select a city, lets say New York on ddTo it is also selected on ddFrom.
In the GUINewBooking.Designer.cs there's only this event handler registered: this.ddFrom.SelectedIndexChanged += new System.EventHandler(this.ddFrom_SelectedIndexChanged);
ddTo has no event handler registered. Any ideas?
public partial class GUINewBooking : Form
{
private CtrlMap ctrlMap;
public GUINewBooking()
{
InitializeComponent();
ctrlMap = new CtrlMap();
ddFrom.DataSource = ctrlMap.GetAllCities();
ddFrom.DisplayMember = "name";
}
private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
{
ddTo.DataSource = ctrlMap.GetAllCities();
ddTo.DisplayMember = "name";
}
}
I believe it's because you are using the same data source. You might need to
private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
{
CtrlMap ctrlMapTo = new CtrlMap();
ddTo.DataSource = ctrlMap2.GetAllCities();
ddTo.DisplayMember = "name";
}
The answer can be found Strange behavior of Windows Forms combobox control
Each combobox DataSource property should be assigned to a different BindingSource object.
Example:
cmbDataType1.DataSource = new BindingSource(datasource, "");
cmbDataType2.DataSource = new BindingSource(datasource, "");
Or in my particular case:
ddFrom.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");
ddTo.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");

My combobox returns incorrect text value

public void DD_Location()
{
var ctx = new LCCDB_EF();
var query = ctx.tbl_Location;
CB_Location.DataContext = query.ToList();
}
private void CB_Location_DropDownClosed(object sender, EventArgs e)
{
textbox_test.Text =CB_Location.Text;
}
Output in Textbox
System.Data.Entity.DynamicProxies.Location_5E43C6C196972BF0754973E48C9C941092D86818CD94005E9A759B70BF6E48E6
Try this
if(null != CB_Location.SelectedItem)
textbox_test.Text = CB_Location.SelectedItem.ToString();
Without seeing your XAML I can't be sure, but are you sure you've bound the list correctly? Try setting the Items property of combobox to your list, rather than the data context. Depending on what the type is and what you'd like to bind the text to, you may need to set the DisplayMemberPath property as appropriate, too.

Can't get value from ComboBox control

I try to use combobox in winforms project.
Here is my code:
private void ShowContoursForm_Load(object sender, EventArgs e)
{
cbxSelectShape.DisplayMember = dataSetObject.ObjectShapes.ShapeNameColumn.ColumnName;
cbxSelectShape.ValueMember = dataSetObject.ObjectShapes.ShapeIDColumn.ColumnName;
cbxSelectShape.DataSource = dataSetObject.ObjectShapes;
}
private void cbxSelectShape_SelectedValueChanged(object sender, EventArgs e)
{
var id= (int)cbxSelectShape.SelectValue;
}
When I choose item from ComboBox SelectedValueChanged is fired,and id variable gets null.
I need to get value of selected item but I always get null in id variable.
Any idea why do I get wrong result and how to fix this code?
You can get the index of ComboBox this way:
private void cbxSelectShape_SelectedValueChanged(object sender, EventArgs e)
{
var id= ((ComboBox)sender).SelectedIndex;
}
You should use SelectedValue property of combobox to get value, associated with ValueMember (ShapeID in your case):
var id = ((ComboBox)sender).SelectedValue;
SelectedIndex returns index of item selected in combobox. Also if this handler used for one combobox, you don't need to cast sender - simply use your combobox variable:
var id = cbxSelectShape.SelectedValue;

FormView Data Binding

I have for fields in my datasource that I want to combine and display in one label field. I have added a procedure to capture the databinding action but I don't how to get the data out of the datasource. I am displaying this information on a FormView is that makes anly difference. Can I get an example in c#?
For example -
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
Payout.Text = datasource.field1 + datasource.field2;
}
I'm not fully sure but it seems like you're looking for something like the following:
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
object dataItem = DataBinder.GetDataItem(FormView1);
Payout.Text = DataBinder.Eval(dataItem, "field1NameHere").ToString() + DataBinder.Eval(dataItem, "field2Namehere").ToString();
}

Categories

Resources