MultiColumn "Results" or listbox in .NET


I'm trying to generate a multicolumn listbox in a VB.NET App. It's part of a search form that will display the results of the search. The problem I'm running into is that there is no way to specify columns I know of in VB.NET. You can use a datagrid I suppose, but I want the end user to be able to select the record populated in the lstbox and open up the record (form) directly from the search form. I have tried casting the columns in the query which does list them all, only way out of format due to spacing, etc... I know there is a way to do this as I've seen many apps that have this function. Are multicolumn list boxes allowed in C#? can't remember.

Below is how I'm attempting to accomplish this and it works, kind of, however, it is not acceptable...

Dim sConnectionString, sSQL As String
        sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\diw07\My Documents\Daily Backups\phone memo backend.mdb"
        sSQL = "SELECT PendingID, PendingClaimNum, Last, First, DatePending, Custodian, [Pending].[PendingClaimNum] & ' ' & [Pending].[Last] & ' ' & [Pending].[First] & ' ' & [Pending].[DatePending] & ' ' & [Pending].[Custodian] As Row FROM Pending WHERE Last Like '" & txtLastName.Text & "'"
        'sSQL = "SELECT PendingClaimNum, Last, First, DatePending, Custodian FROM Pending WHERE Last Like '" & txtLastName.Text & "'"
        Dim conn As New System.Data.OleDb.OleDbConnection(sConnectionString)
        Dim cmd As New System.Data.OleDb.OleDbCommand(sSQL, conn)
        Dim dr As System.Data.OleDb.OleDbDataReader
        Dim dat As OleDbDataAdapter = New OleDbDataAdapter(sSQL, conn)
        Dim dt As New DataSet()
        dat.Fill(dt, "Pending")
        lstResults.DataSource = dt.Tables("Pending").DefaultView
        lstResults.DisplayMember = "Row"
        lstResults.ValueMember = "PendingID"

Open in new window

Who is Participating?
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
forget the DisplayMember and the ValueMember. The ListView or the Datagrid are more complex object due to the fact that they can hold multiple columns.

For listview, have a look at:

For the DataGrid:

If you would like a multi-column ComboBox, have a look at
Éric MoreauSenior .Net ConsultantCommented:
Sure you can use the datagrid, but you could also use a ListView. Any control you use, you will have to retreive the key of the selected row to open the other form
c9k9hAuthor Commented:
And that is where I'm a bit lost (in retrieving the Key). The Key is Obviously the .ValueMemeber (PendingID). The listView will not allow me to use .DisplayMember or .ValueMember, however the Datagrid will.

Is there a way to restructure this so that I can at least get the listview to work?
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Éric MoreauSenior .Net ConsultantCommented:
no but you have access to all the columns. You need to store the key in one of the columns (of the listview or the grid) even if you set its width to 0 so that it is not visible.
c9k9hAuthor Commented:
Okay, that is what I have done in Access (previous platform)

Perhaps i'm not familiar enough with the listview properties?

How would I go about storing the key? The only way I know of populating the listbox is with the above code.. and this:

lstResults.DataSource = dt.Tables("Pending").DefaultView
        lstResults.DisplayMember = "Row"
        lstResults.ValueMember = "PendinfgID"
c9k9hAuthor Commented:
Okay - I think this will get me a lot further!

Thank you! One more question...  What do I need to import for the "ListItem" It's coming up as not defined.
c9k9hAuthor Commented:
Actually... That's not what i need... I want to inherit the columns from the query that feeds the view.
Éric MoreauSenior .Net ConsultantCommented:
what do you mean by import? Do you code to show?
c9k9hAuthor Commented:
Well I just found the following code that looks like it's doing what I want... Perhaps for a web app or something though?

It looks as though it's importing the column headers from the query.. Ultimately this is what i'm trying to do.

private void cmdExecute_Click(object sender, System.EventArgs e) {
     SqlConnection conn = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
     try {
       lvwResult.Columns.Clear() ;
       txtSql.Text ="select * from Employee";
       SqlCommand cmd = conn.CreateCommand();
       cmd.CommandText = txtSql.Text;
       SqlDataReader dr = cmd.ExecuteReader();
       for (int i = 0; i< dr.FieldCount; i++) {
         ColumnHeader ch = new ColumnHeader();
       ListViewItem itmX; 
       while (dr.Read()) {
         itmX=new ListViewItem(); 
         itmX.Text= dr.GetValue(0).ToString();
         for (int i=1 ; i< dr.FieldCount; i++) {
    } catch ( System.Data.SqlClient.SqlException  ex) {
       Console.WriteLine("There was an error in executing the SQL." +
               "\nError Message:" + ex.Message, "SQL");
    } finally {

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
it doesn't seems to be for a web app. but it is in c#. have you been able to make it work? what about the samples I have provided in VB?
c9k9hAuthor Commented:
Yup, it sure is C#... I have not been able to make it work yet... It will probably be more trouble than re-coding it in VB.

The samples you provided are great... I'm just not sure how to piece them together yet. It looks like one of the examples you provided could work... Just not sure how. What is a SQLDataReader? I'm assuming this is a control that you create from a wizard or something? I guess I'm just old fashion.

It's this code...

 Dim itmListItem as ListViewItem
   Dim shtFieldCntr As Short

   Do While myData.Read
      itmListItem = New ListViewItem()

      If myData.IsDBNull(myData(0)) Then
          itmListItem.Text = ""
          itmListItem.Text = myData(0)
      End If

      For shtFieldCntr = 1 To myData.FieldCount() - 1
          If myData.IsDBNull(shtFieldCntr) Then
         End If
      Next shtCntr

c9k9hAuthor Commented:
You know what
I think i got it with the references you have provided!


If not.. I'll post again, but you have been more than helpful!!!
All Courses

From novice to tech pro — start learning today.