I have a SQL database which contain user Name column and last name column i manage to add the values to the combo box by using "datareader" but now the two of the Values are in the combo box.The value Rudi Steelman is in the combo box(The user name is Rudi last name is Steelman both are in the same row) i want to show both the values in the combo box in the same row(that i can manage by datareader) but i only want to select the value rudi for textbox1 how can i do this is there another way?
How to select items in a combo box
Hi I thank again for the answer that this site gave me for the last question.
I have a SQL database which contain user Name column and last name column i manage to add the values to the combo box by using "datareader" but now the two of the Values are in the combo box.The value Rudi Steelman is in the combo box(The user name is Rudi last name is Steelman both are in the same row) i want to show both the values in the combo box in the same row(that i can manage by datareader) but i only want to select the value rudi for textbox1 how can i do this is there another way?
I have a SQL database which contain user Name column and last name column i manage to add the values to the combo box by using "datareader" but now the two of the Values are in the combo box.The value Rudi Steelman is in the combo box(The user name is Rudi last name is Steelman both are in the same row) i want to show both the values in the combo box in the same row(that i can manage by datareader) but i only want to select the value rudi for textbox1 how can i do this is there another way?
Bryian TanPosted May 29, 2011, 12:44 AM
You can bind the ComboBox with object as well instead of string. And then you can reference the selected value from code behind. Here are the sample codes.
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c7a82a6a-763e-424b-84e0-496caa9cfb4d/
http://www.c-sharpcorner.com/Forums/Thread/120655/combo-box-value-and-text-in-vs-2008-for-windows-applicat.aspx
VulpesPosted May 28, 2011, 12:32 PM
combox_User.Items.Add(userName + " " + lastName);
Otherwise you need some way to determine where the second word starts. Would it be safe to assume that it always begin with an upper case letter? Is so, you could do it like this:
string name = combox_User.SelectedItem.ToString();
int index = 0;
for(int i = 1; i < name.Length; i++)
{
char c = name[i];
if (Char.IsUpper(c))
{
index = i;
break;
}
}
textBox1.Text = name.Substring(0, index); // to get first name
Rudi SteelmanPosted May 28, 2011, 11:45 AM
reader = comuser.ExecuteReader();
while(reader.Read ())
{
string userName,lastName;
userName = reader [0].ToString();
lastName = reader [1].ToString();
combox_User.Items.Add(userName+lastName);
}
VulpesPosted May 26, 2011, 12:38 PM