How do I get the combobox value?
I usually do web programming, but have a requirement for a window form in this project that I am currently working on.
I am using SubSonic to get the task list and download the combobox:
JobCollection jobs = new JobCollection().Load();
cboJobs.DisplayMember = "Name";
cboJobs.ValueMember = "Id";
cboJobs.DataSource = jobs;
When the user selects an item and clicks the search button, I need to query the database using the "Id" field of the job.
TransactionCollection transactions = new TransactionCollection();
if (!String.IsNullOrEmpty(txtTransactionId.Text))
{
transactions.Where("id", txtTransactionId.Text);
}
else
{
transactions.Where("raw_data", Comparison.Like, "%" + txtFind.Text + "%");
}
transactions.Where("job_id", Convert.ToInt32(cboJobs.SelectedItem));
transactions.Load();
The line that has the problem is the one that goes "Convert.ToInt32 (cboJobs.SelectedItem)".
How do I get the job ID compared to the name?
a source to share
You might want to look at the property of SelectedValue
your field. You are setting the ValueMember to the ID, so it SelectedValue
must contain the correct value.
a source to share
You already got a (good) answer, but I thought it might be in place to point it out anyway: in the app combo box, each item will actually be a complete Job object, not just a string representation of one. property SelectedItem
returns the Job object that is currently selected in the ComboBox. You can access any of your members by simply dropping it in Job:
Job currentJob = cboJobs.SelectedItem as Job;
if (currentJob != null)
{
// here you can access currentJob.Name, currentJob.Id or any other member
}
a source to share