Solved

MultiColumn "Results" or listbox in .NET

Posted on 2009-04-09
12
298 Views
Last Modified: 2013-11-26
Hello,

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...

THANKS!!!
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

        conn.Open()

        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"

        conn.Close()

Open in new window

0
Comment
Question by:c9k9h
  • 7
  • 5
12 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 24112636
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
0
 

Author Comment

by:c9k9h
ID: 24115427
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?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 24115481
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.
0
 

Author Comment

by:c9k9h
ID: 24115607
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"
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 24115702
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:
http://www.codeguru.com/vb/controls/vbnet_controls/listview/article.php/c3979
http://www.ondotnet.com/pub/a/dotnet/2002/10/28/listview.html?page=1

For the DataGrid:
http://visualbasic.about.com/od/learnvbnet/ss/ecvbsbs2101.htm
http://www.codeproject.com/KB/database/grid101.aspx

If you would like a multi-column ComboBox, have a look at http://www.emoreau.com/Entries/Articles/2005/02/Multi-columns-ComboBox.aspx
0
 

Author Comment

by:c9k9h
ID: 24116389
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:c9k9h
ID: 24116469
Actually... That's not what i need... I want to inherit the columns from the query that feeds the view.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 24116476
what do you mean by import? Do you code to show?
0
 

Author Comment

by:c9k9h
ID: 24116578
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() ;

       lvwResult.Items.Clear();
 

       conn.Open();

       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();

         ch.Text=dr.GetName(i);

         lvwResult.Columns.Add(ch);

       }
 

       ListViewItem itmX; 
 

       while (dr.Read()) {

         itmX=new ListViewItem(); 

         itmX.Text= dr.GetValue(0).ToString();
 

         for (int i=1 ; i< dr.FieldCount; i++) {

            itmX.SubItems.Add(dr.GetValue(i).ToString());

         }

         lvwResult.Items.Add(itmX);

       }

       dr.Close();

    } catch ( System.Data.SqlClient.SqlException  ex) {

       Console.WriteLine("There was an error in executing the SQL." +

               "\nError Message:" + ex.Message, "SQL");

    } finally {

       conn.Close();

    }

  }

}

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 24116658
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?
0
 

Author Comment

by:c9k9h
ID: 24116840
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 = ""
      Else
          itmListItem.Text = myData(0)
      End If

      For shtFieldCntr = 1 To myData.FieldCount() - 1
          If myData.IsDBNull(shtFieldCntr) Then
             itmListItem.SubItems.Add("")
          Else
             itmListItem.SubItems.Add(myData.GetString(shtFieldCntr))
         End If
      Next shtCntr

      MyListView.Items.Add(itmListItem)
   Loop
0
 

Author Comment

by:c9k9h
ID: 24116935
You know what
I think i got it with the references you have provided!


THANK YOU SOOOO MUCH!!!!

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now