Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 917695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:15:04+00:00 2026-05-15T18:15:04+00:00

I have a table in SQL Server which looks like this: ID Code Name

  • 0

I have a table in SQL Server which looks like this:

ID Code Name     Surname
1   MS  Mike     Smith 
2   JD  John     Doe
3   UP  Unknown  Person

and so on…

Now I want to bind the data from this table into the ComboBox in a way that in the ComboBox I have displayed value from the Code column.

I am doing the binding in this way:

SqlDataAdapter sqlAdapter = new SqlDataAdapter("SELECT * FROM dbo.Users ORDER BY Code", MainConnection);
sqlAdapter.Fill(dsUsers, "Users");
cbxUsers.DataSource = dsUsers.Tables["Users"];
cmUsers = (CurrencyManager)cbxUsers.BindingContext[dsUsers.Tables["Users"]];
cbxUsers.DisplayMember = "Code";

And this code seems to work. I can scroll through the list of Codes. Also I can start to write code by hand and ComboBox will autocomplete the code for me.

However, I wanted to put a label at the top of the combobox to display Name and Surname of the currently selected user code.

My line of though was like that: “So, I need to find an event which will fire up after the change of code in combobox and in that event I will get the current DataRow…”

I was browsing through the events of combobox, tried many of them but without a success.

For example:

private void cbxUsers_SelectionChangeCommitted(object sender, EventArgs e)
{
 if (cmUsers != null)
 {
  DataRowView drvCurrentRowView = (DataRowView)cmUsers.Current;
  DataRow drCurrentRow = drvCurrentRowView.Row;
  lblNameSurname.Text = Convert.ToString(drCurrentRow["Name"]) + " " + Convert.ToString(drCurrentRow["Surname"]);
 }
}

This give me a strange results. Firstly when I scroll via mouse scroll it doesn’t return me the row wich I am expecting to obtain. For example on JD it shows me “Mike Smith”, on MS it shows me “John Doe” and on UP it shows me “Mike Smith” again!
The other problem is that when I start to type in ComboBox and press enter it doesn’t trigger the event.

However, everything works as expected when I bind data to lblNameSurname.Text in this way:

lblNameSurname.DataBindings.Add("Text", dsusers.Tables["Users"], "Name");

The problem here is that I can bind only one column and I want to have two. I don’t want to use two labels for it (one to display name and other to display surname).

So, what is the solution to my problem?

Also, I have one question related to the data selection in ComboBox. Now, when I type something in the combobox it allows me to type letters that are not existing in the list.
For example, I start to type “J” and instead of finishing with “D” so I would have “JD”, I type “Jsomerandomtexthere”. Combobox will allow that but such item does not exists on the list. In other words, I want combobox to prevent user from typing code which is not on the list of codes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:15:05+00:00Added an answer on May 15, 2026 at 6:15 pm

    Just did a test on this myself, and I think what you might be looking for is bound to the wrong event (maybe even the wrong object).

    I just tried adding the event to the PositionChanged handler of the CurrencyManager object (cmUsers), and it worked exactly as intended. The only issue I had with this was the first time it’s loaded, it doesn’t hit the PositionChanged so the first item never gets bound to the label (I’m sure there’s an easier fix).
    http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingmanagerbase.positionchanged.aspx

    Ran a little test to see which event got fired first, and it seems the SelectionChangeCommitted event fired before the PositionChanged, meaning the CurrencyManager hasn’t updated it’s internal position yet, so the line:

    DataRowView drvCurrentRowView = (DataRowView)cmUsers.Current;
    

    Pulls the wrong DataRow.

    Forgot about your second issue. Most controls do not handle keypresses, so what you should do is bind the OnKeyDown or OnKeyPress events to run whatever function you want (finding the index associated with the typed object [ie. FindString()], and set the ComboBox.SelectedIndex property to the found index, otherwise flash a warning). For example:

    if(e.KeyCode == Keys.Enter)
    {
        int indx;
        cmbBox.SelectAll();
        if((indx = FindString(cmbBox.SelectedText)) != -1)
            cmbBox.SelectedIndex = indx;
        else
            // Some warning
    }
    

    Edit
    After reading your comments, I figured an easier way to do the validation. In the “KeyPress” event of the ComboBox, use the following code:

    if(e.KeyChar != (char)Keys.Enter)
    {
        if(cmbBox.FindString(cmbBox.Text + e.KeyChar) == -1)
        {
            e.Handled= true;
        }
    }
    

    No fuss, no muss. Since we’re limiting to only items in the list here, we don’t need to handle when they press Enter as it should automatically change the selected item (however, it wouldn’t hurt to run some validation to make sure it’s the proper cAsE, or just changed the SelectedIndex property manually on Enter keypress).
    Still, hope this helped someone out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use developer tool in chrome to explore the DOM :… May 17, 2026 at 1:13 am
  • Editorial Team
    Editorial Team added an answer From the docs for vector::resize: _Val: The value of new… May 17, 2026 at 1:13 am
  • Editorial Team
    Editorial Team added an answer Unfortunately, as of VS 2008, not really. I'm not sure… May 17, 2026 at 1:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a CSV dump from another DB that looks like this (id, name,
I have a couple of tables which look like this Table 1 user_id |
I have SQL Server 2008 and VS 2008 Pro. And I am coding in
HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
I have an Access 2003 project in which all data is stored in SQL
I am working on the Full Text Search in Sql Server 2005. I have
I have a Country table which has CountryName column. I need a ready made
I am looking for T-SQL code to list all tables in all databases in
I have a really weird issue with an SQL query I've been working with
I'm working on a .NET 4 application, C#, Entity Framework 4, SQL Server 2008.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.