DataBinding Through Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wbwalsh
    New Member
    • Mar 2010
    • 1

    DataBinding Through Code

    Hi all. This is my first post… be gentle :)

    I'm not looking for the code to make it work... if you could just point me in the right direction... I am self-taught and really need to learn by doing rather then having it given to me. Thanks all!

    I have a WinForm with a single ListBox and several TextBoxes. I have an SQL database in the project. I am currently using a connection string to open and read the data filling a dataTable. I have set my ListBox Source and DisplayMember which are working properly. My ListBox displays LastName, FirstName as it should by combining the LastName and FirstName fields in the commandString.

    My issue is how I achieve dataBinding/concurrency without using “drop and drag” binding within Visual Studio?

    I have to cycle through the records in the ListBox and have the corresponding TextBoxes display the record information. I understand using the SelectedIndexCh anged event for the listbox but I’m missing the “piece” to make the whole thing work.

    Here is the code to open, read and fill listbox:

    Code:
    string connectionString = "Data Source=walsh\\sqlexpress;Database=PPDOne_db;Integrated Security=True;";
                SqlConnection conn = new SqlConnection(connectionString);
                string commandString = "SELECT *, NameLast +', ' + NameFirst AS NameFull FROM tblAdvocates ORDER BY NameLast";
                SqlDataAdapter dataAdapter = new
                SqlDataAdapter(commandString, conn);
                DataSet ds = new DataSet();
                dataAdapter.Fill(ds, "prog");
                dataTable = ds.Tables["prog"];
                lbxAdvocates.DataSource = ds.Tables["prog"].DefaultView;
                lbxAdvocates.DisplayMember = "NameFull";
                lbxAdvocates.SetSelected(0, true);
                conn.Close();
    Any help would be appreciated!
    Last edited by tlhintoq; Mar 30 '10, 06:23 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    Working...