Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

MultiColumn "Results" or listbox in .NET

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
c9k9h
Asked:
c9k9h
  • 7
  • 5
1 Solution
 
É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
0
 
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?
0
 
É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.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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"
0
 
Éric MoreauSenior .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:
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
 
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.
0
 
c9k9hAuthor Commented:
Actually... That's not what i need... I want to inherit the columns from the query that feeds the view.
0
 
Éric MoreauSenior .Net ConsultantCommented:
what do you mean by import? Do you code to show?
0
 
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() ;
       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
 
É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?
0
 
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 = ""
      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
 
c9k9hAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now