Link to home
Start Free TrialLog in
Avatar of bowemc
bowemc

asked on

Databinding Textbox & combo

Hi I have a c# dataset in a winform application usint .net 1.1. I have a combobox and a textbox on my form. My combo box is displaying the results of column 1 of the dataset.

I want ti so that as the selected index of the combo i change the respective entry in column 2 of the dataset is loaded into my text box (so it appears that the text box is bound)

Thanks
Avatar of Pratima
Pratima
Flag of India image

For this you can bind the the column 1 to the datatextfiled of the combo box & in datavaluevalue field as the column2 when you are binding dataset to the combobox.

then on select inedx change you just need to get the selected value and put it into the textbox
Avatar of bowemc
bowemc

ASKER

Hi - Thanks for the fast response. I'm using .net 1.1I can't see the combo's datatextfiled and datavaluevalue  with telesense. Below is How I'm currently binding the combo. The way I have the textbox coded all I ever see is the column name rather than the desired respective description. THANKS

            public frmChangeHeadder()
            {
                  InitializeComponent();

                  dsHeadder = _cmd.PageHeadders();

                  cboHeadderName.DisplayMember = dsHeadder.Tables[0].Columns[1].ColumnName;
                  cboHeadderName.ValueMember = dsHeadder.Tables[0].Columns[1].ColumnName;
                  cboHeadderName.DataSource = dsHeadder.Tables[0];
            }


            private void cboHeadderName_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  txtHeadderDescription.Text = dsHeadder.Tables[0].Columns[2]..ToString();
            }

DataTable FTable =
                        new DataTable();
                  System.Data.SqlClient.SqlDataAdapter FAdp = new System.Data.SqlClient.SqlDataAdapter
                        (AQuery,obj.Connection);
                  FAdp.Fill(FTable);
                  comboBox1.DataSource = FTable;
                  comboBox1.DisplayMember = "PRODUCTID";
                  comboBox1.ValueMember = "PRODUCTID";
                  textBox1.DataBindings.Add
                        ("Text",FTable,"PRODUCTID");
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Text field means DisplayMember and value means ValueMember
Avatar of bowemc

ASKER

pratima_mcs - thanks this seems to work

Only problem is later in my code I need to read the text value displayed in my combo. Whn I use cboHeadderName.SelectedValue.ToString() i get back the value in column 2 - i need column 1. How can I do this without affecting what we have already set up? Thanks again
There you can use

cboHeadderName.SelectedText.ToString()
OR
cboHeadderName.SelectedItem.Text.ToString()
Avatar of bowemc

ASKER

Firstly I have nooption for Text after cboHeadderName.SelectedItem.

Next cboHeadderName.SelectedText.ToString(); returns "" , cboHeadderName.SelectedItem.ToString(); returns System.Data.DataRowView and cboHeadderName.SelectedValue.ToString(); as expected returns column2
Avatar of bowemc

ASKER

This seems to have done the trick

                  string str_id_header = "";
                  System.Data.DataRowView ownerVal = (System.Data.DataRowView) cboHeadderName.SelectedItem;      
                  str_id_header = ownerVal.Row.ItemArray[1].ToString();

This is a clikd windo. When I complete a database update ugin the data I have the child window dissaears / closes. Any ideas why?