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
C#

Avatar of undefined
Last Comment
bowemc
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Pratima
Pratima
Flag of India image

Text field means DisplayMember and value means ValueMember
Avatar of bowemc
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
Avatar of Pratima
Pratima
Flag of India image

There you can use

cboHeadderName.SelectedText.ToString()
OR
cboHeadderName.SelectedItem.Text.ToString()
Avatar of bowemc
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
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?
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo