Listbox databinding

Listbox databinding

Post by Chuc » Sat, 27 Jul 2002 07:12:46



I have a windows form that displays records from a
database.  One of the fields has lookup values in a
dropdownlist box.

What is the best way to fill the list box initially
  1. While DR.read, listbox.add
  2. Bind the list box to the record set.

After the list box is filled what is the best way to set
the listbox display the value in the recordset that I want
to edit.
Also what is the best way to get the displayed value to be
linked to my record set during update.

 
 
 

Listbox databinding

Post by Steven Bras [M » Sat, 27 Jul 2002 07:51:57


As far as the databinding is concerned, I would use a reader and fill the
listbox. This way, you avoid the overhead of a DataAdapter.

Example:

'-- Begin sample code
        Dim oCn As New SqlConnection("data
source=(local);database=Northwind;user id=me;")
        Dim oCmd As New SqlCommand("SELECT CompanyName FROM Customers", oCn)

        oCn.Open()

        Dim oRdr As SqlDataReader
        oRdr = oCmd.ExecuteReader(CommandBehavior.CloseConnection)

        While oRdr.Read
            ListBox1.Items.Add(oRdr(0))
        End While

        oRdr.Close()
        oCmd.Dispose()
        oCn.Close()
        oCn.Dispose()
'-- End sample code

To move the selection to an item in the list, use the FindString and
FindStringExact methods, each of which takes a string as a parameter.

Not clear on what you mean by getting the displayed value to be linked to
your recordset during update; I think you may mean databinding, in which
case you want to add a databinding to the databindings collection that
binds the SelectedText property to your dataset.

Steven Bras, MCSD
Microsoft Developer Support/Visual Basic WebData

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

 
 
 

Listbox databinding

Post by Chuc » Sat, 27 Jul 2002 23:09:59


Thanks, for the databinding to the recordset.  I have
listbox filled now, so now I want to display and edit a
record from some sort of table (maybe the listbox has
states and the editing records is personnel addresses).

How do I bind the listbox to the personnel record set so
as to display the personnel record?  Or so that if the
state is changed in the listbox, when I save the personnel
recordset it is updated.  Or do I have to do some sort of
coding vb6 style to set and retrieve the values.

thanks,

Quote:>-----Original Message-----
>As far as the databinding is concerned, I would use a

reader and fill the
Quote:>listbox. This way, you avoid the overhead of a
DataAdapter.

>Example:

>'-- Begin sample code
>        Dim oCn As New SqlConnection("data
>source=(local);database=Northwind;user id=me;")
>        Dim oCmd As New SqlCommand("SELECT CompanyName

FROM Customers", oCn)
Quote:

>        oCn.Open()

>        Dim oRdr As SqlDataReader
>        oRdr = oCmd.ExecuteReader

(CommandBehavior.CloseConnection)
Quote:

>        While oRdr.Read
>            ListBox1.Items.Add(oRdr(0))
>        End While

>        oRdr.Close()
>        oCmd.Dispose()
>        oCn.Close()
>        oCn.Dispose()
>'-- End sample code

>To move the selection to an item in the list, use the
FindString and
>FindStringExact methods, each of which takes a string as
a parameter.

>Not clear on what you mean by getting the displayed value
to be linked to
>your recordset during update; I think you may mean

databinding, in which
Quote:>case you want to add a databinding to the databindings
collection that
>binds the SelectedText property to your dataset.

>Steven Bras, MCSD
>Microsoft Developer Support/Visual Basic WebData

>This posting is provided "AS IS" with no warranties, and
confers no rights.

>Are you secure? For information about the Microsoft

Strategic Technology

- Show quoted text -

Quote:>Protection Program and to order your FREE Security Tool
Kit, please visit
>http://www.microsoft.com/security.

>.

 
 
 

Listbox databinding

Post by Steven Bras [M » Sun, 28 Jul 2002 07:43:33


I see what you're trying to do. Here's another sample:

1. Open a new Windows forms project in VS.Net;
2. Add a listbox and a command button to the default form1;
3. Add the following code to the form1 class:

'-- Begin sample code
Imports System.Data.SqlClient

Public Class Form1

    Inherits System.Windows.Forms.Form

    Private oCn As SqlConnection
    Private oDa As SqlDataAdapter
    Private oDs As DataSet
    Private oCm As CurrencyManager

'# Windows Form Designer Code

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        oCn = New SqlConnection("data
source=(local);database=Northwind;user id=me;")
        Dim oDaList As New SqlDataAdapter("SELECT CompanyName FROM
Customers", oCn)
        Dim oDsList As New DataSet()

        oDaList.Fill(oDsList, "Customers")
        ListBox1.DataSource = oDsList.Tables("Customers")
        ListBox1.DisplayMember = "CompanyName"
        ListBox1.ValueMember = "CompanyName"

        'Now bind the list box to another dataset
        oDa = New SqlDataAdapter("SELECT * FROM Customers", oCn)
        oDs = New DataSet()
        oDa.Fill(oDs, "Customers")
        oCm = CType(Me.BindingContext(oDs.Tables("Customers")),
CurrencyManager)

        ListBox1.DataBindings.Add("SelectedValue", oDs.Tables("Customers"),
"CompanyName") '<this keeps the list selection in sync
        TextBox1.DataBindings.Add("Text", oDs.Tables("Customers"),
"ContactName")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        oCm.Position += 1
    End Sub
'-- End sample code

Change the connection string to point to your SQL Server database. Run the
project; click the button; you will see that the company names related to
the contact name in the textbox are selected as you move through the
records. If you change the selected listbox row, the company name will be
updated in the database (the update code for this is not shown; all you
should have to do is call oDA.Update to save the changes to the database.

This example is taken from MSDN online help, in a section titled "Creating
a Lookup Table for a Windows Forms ComboBox, ListBox, or CheckedListBox
Control". I recommend reviewing it for more details and step by step
instructions.

Good luck!

Steven Bras, MCSD
Microsoft Developer Support/Visual Basic WebData

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

 
 
 

1. ATTN: Microsoft - Listbox Databinding Bug?

Hi,

I'm trying to bind a listbox to a strongly typed dataset using the following code:

lstNotes.DataSource = mo_CIDataSet; // mo_CIDataSet is an existing strongly typed dataset
lstNotes.DisplayMember = "Customers.Customer-CustomerNotes.NoteTitle";
lstNotes.ValueMember = "Customers.Customer-CustomerNotes.NoteID";

Customers is a table, Customer-CustomerNotes is a relationship between the Customers and CustomerNotes tables

The list box renders this just fine.  I've attached and event handler to the DataTable's ColumnChanging event to handle data validation.  In the event that there is invalid data being entered into a specific column, I call the DataRow's SetColumnError() method, and pass it an error string.

Once this happens, the Listbox re-renders itself with additional, blank items in the list.  Two, when the error is removed by passing a blank string to the DataRow's SetColumnError() method, the list box does remove some of the additional blank items, but does not re-render the Display Member in each items display text.

Am I'm doing something wrong or forgetting something, or is this a bug?

Thanks in advance,

Jordan

2. \listoffigures and \listoftables with Appendix numbering?

3. listbox databinding techniques

4. 10ms DTR toggle doesn't reset modem

5. RC1, Listbox databinding

6. GPFax+USR problems

7. Listbox (Databound) problem

8. How to handle milliseconds in datetime f ield

9. Listbox DataBinding bug

10. Listbox databinding

11. Databinding of listbox with different tables

12. DataBindings ComboBox and ListBox

13. Databinding Multiselect Listbox