[dba-VB] Listbox on VB.Net Form - How to Display Data Correctly?

Gustav Brock Gustav at cactus.dk
Wed May 18 05:30:27 CDT 2011


Hi Dan

At the row enter event you may need to include code to clear the combo.
C# only, but you sure get the picture:

       private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // Row to look up is the current row.
            int rowIndex = e.RowIndex;
            // Column in row to look up Id of Supplier.
            int columnId = 0;

            _supplierId = Convert.ToInt32(dataGridView1.Rows[rowIndex].Cells[columnId].Value);

            this.FindCurrentCustomer();
        }

        private void FindCurrentCustomer()
        {
            DataRow[] customerSupplier = _customerSupplierDataTable.Select("SupplierId = " + _supplierId.ToString());
            if (customerSupplier.Length == 0)
            {
                _customerId = -1;
                // Erase display.
                this.comboBoxCustomer.Text = null;
            }
            else
            {
                _customerId = Convert.ToInt32(customerSupplier[0]["CustomerId"].ToString());
            }
            this.comboBoxCustomer.SelectedValue = _customerId;
        }

/gustav


>>> "Dan Waters" <df.waters at comcast.net> 17-05-2011 23:00 >>>
I'm making my first .net form with existing data and I'm having trouble
getting the listbox to display correctly.  Been looking through all my books
and doing lots of web searches, but no real answer.

 

The form has a TurnbackMainBindingSource which uses a DataSource named
TurnbackMain.  TurnbackMain is an object in a Linq to SQL DataContext (.dbml
file).

 

I have two list boxes, both of which use an items collection entered via
property sheet.  The Severity listbox has choices 1, 2, 3.  The QD listbox
has choices Quality, Delivery.  Both listboxes, have the SelectedItem set to
TurnbackMainBindingSource - Severity and TurnbackMainBindingSource - QD,
respectively.  The comboboxes and textboxes have similar values in the Text
property, but the listbox does not have a Text property in the property
sheet.

 

As I use the Navigation toolbar to move the form through the records, I want
the listboxes to display like Access does, which I assume is the way a
listbox should in .Net as well.

 

What happens:

 

    For the QD listbox, as I scroll through the records, the Quality or
Delivery item is highlighted, if the underlying data is not null.  If the
underlying data is null, then whatever was highlighted for the previous
record remains highlighted.

 

    For the Severity listbox, there is no highlighting at all.

 

    For the comboboxes and textboxes, what's displayed matches the
underlying data.

 

The underlying data for QD is string, the underlying data for Severity is
numeric.

 

Is there a way to make this happen correctly without writing any code in the
form's afterupdate event?  If no, then what code is needed?

 

Thanks!

Dan





More information about the dba-VB mailing list