Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MultiColumn "Results" or listbox in .NET

Posted on 2009-04-09
12
Medium Priority
?
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 70

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 70

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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 

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 70

Accepted Solution

by:
Éric Moreau earned 2000 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
 

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 70

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 70

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

715 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