Solved

MultiColumn "Results" or listbox in .NET

Posted on 2009-04-09
12
301 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 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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