Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day

I am trying to populate a text box based on a combo box selection,and all I am getting in the text box is "System.Data.DataRowview". below is my code.

-this is the code that populates the combo boxes
C#
private void frmEmployeesJobs_Load(object sender, EventArgs e)
 {
     string empSql = string.Format("Select  (CAST(employee_id AS VARCHAR(MAX))+ ' - '+employee_name) as employee FROM employee");

     DataTable empTable = FillTable(empSql);
     cmbEmp.DataSource = empTable;
     cmbEmp.DisplayMember = "employee";

     string jobSql = string.Format("Select  (CAST(job_id AS VARCHAR(MAX))+ ' - '+job_name) as job, job_price  FROM job");

     DataTable jobTable = FillTable(jobSql);
     cmbJob.DataSource = jobTable;
     cmbJob.DisplayMember = "job";

 }


this is the code that attempts to fill the textbox.

C#
private void cmbJob_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbJob.SelectedIndex == -1)
            {
                txtPrice.Text = string.Empty;
            }
            else
            {
              
                txtPrice.Text = cmbJob.SelectedItem.ToString();
              
            }

basically what i want to do is fill the price textbox when ever a job is selected
        }


Thanks in advance
Posted

Hi,

just try this.
C#
txtPrice.Text = cmbJob.SelectedText.ToString(); //if selected Text to be displayed

txtPrice.Text = cmbJob.SelectedValue.ToString(); // if selected value to be displayed

hope it helps.
 
Share this answer
 
v2
Comments
Georges23 12-Feb-13 6:28am    
Karthik Harve there is no such thing in c sharp thanks
Karthik Harve 12-Feb-13 6:35am    
Does updated answer works ?
Jibesh 12-Feb-13 6:53am    
No it wont work. You need to set the DisplayMember and ValueMember to get the data. See my solution.
Karthik Harve 12-Feb-13 6:58am    
yes. i did't saw the code lines, which are binding the combobox. you are correct. thanks.
If you are already bind a datasource to combo you can use the combo.SelectedValue property instead of SelectedItem.
i.e
C#
txtPrice.Text = cmbJob.SelectedValue.ToString();


also the set the cmbJob.ValueMember = "job"; do the same for other combobox.
 
Share this answer
 
v2
Comments
Georges23 12-Feb-13 6:36am    
Jibesh it gives me this value in the textbox when i do what you said. "System.Data.DataRowview"
Georges23 12-Feb-13 6:37am    
no nothing works
Jibesh 12-Feb-13 6:38am    
moment.. i re-call something.. you need to set ValueMember also.. let me check it.. do read about.

http://msdn.microsoft.com/en-us/library/microsoft.dynamics.mobile.framework.controls.combobox.valuemember(v=mag.11).aspx
Georges23 12-Feb-13 6:41am    
how do i do that ?
Jibesh 12-Feb-13 6:51am    
just like the way you use DisplayMember, set the ValueMember property. see my updated solution.

The reason why you need to set both DisplayMemeber and ValueMemeber is, it is possible to use different values for display and for internal processing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900