Solved

How do I display a two-dimensional array?

Posted on 2009-07-14
6
158 Views
Last Modified: 2012-05-07
Hi,

I have a 2-d array which I want to display on the screen. It contains the names of technicians and the number of tickets they resolved. I want to be able to print the array as two columns. I'm trying to print to a text box, but it is only printing the last entry in the array. Heres the code

       Do While counter <> -1
            TextBox1.Text = array(counter, 0) & "   " & array(counter, 1) & vbCrLf
            counter = counter - 1
        Loop

Where "counter" is the upper bound of the array at the start..

Alternatively, can I do this using datagridview?

Thanks
0
Comment
Question by:OPT Expert
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:NotLogical
ID: 24852902
Hi optrust,

The reason that you only see the last line, is due to the statement:

  TextBox1.Text = ....

This has the effect of replacing the ENTIRE contents of the text box, on every iteration through the loop, with only the current line being indexed by the counter variable.

You will have better results by replacing it with:

   TextBox1.Text = TextBox1.Text + ....

I do not know how your text box is setup, or whether the form you are using is persistent (it is on-screen at all times). You will also want to add this line, before the Do While:

   TextBox1.Text = ""

This will make sure that any previous results are cleared from the text box.

Yes, you could do this with a datagrid - it would look much cleaner...
0
 

Author Comment

by:OPT Expert
ID: 24852953
I'm trying to do it with a datagrid.. What steps do I have to go through? I've assigned to datasource to the array, but what next?
0
 

Author Comment

by:OPT Expert
ID: 24853087
Oh, and also thanks. I completely forgot about appending to whatever was in the box. It works but its messy, which is why I want to go with the datagrid. Do you know how I could do this?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Accepted Solution

by:
NotLogical earned 500 total points
ID: 24856540
No problem!

The only small issue, is that I am not sure which version of VB you are using... I currently only have VS2005, so I am basing the solution on the component names therein.

I am also assuming that your array, is an array of string().

Here are the steps, to do this manually.
- First, create your DataGridView, and name it (in my case, dgvResults).
- Add your two columns. I added a column Names and Count. I'll show you why this is useful in a sec...
- Your loop should look like this:

   Do While counter <> -1
      dgvResults.Rows.Add( array( counter, 0 ), array( counter, 1 ) )
      counter = counter - 1
   Loop

That should be it...

Now, a useful reason for naming the columns, is you can do something like this:

   dgvResults.Item("Names", 3).Value = "Some other name"
   dgvResults.Item("Count", 3).Value = 27
   dgvResults.Item("Count", 3).Style.BackColor = Color.Gold

You do not need to keep track of which column index refers to which data... Very handy.

Note: in the above example, I am setting values of row #4. If there is no data in this DataGridView, this will fail, as I cannot just update data on a row that does not yet exist! I am sure that you get the idea on how this can be useful...

All of the above steps can also be done in code. If you are interested, we can leave that to another post...

Cheers,

NotLogical
0
 

Author Comment

by:OPT Expert
ID: 24860525
Hey,

Thanks for the reply.

I'm using VB2008. When I create the datagridview, it gives me an error in the form designer saying

Error      1      Value of type 'String' cannot be converted to 'System.Windows.Forms.DataGridViewTextBoxColumn'.      

Any ideas?
0
 
LVL 8

Expert Comment

by:NotLogical
ID: 24861018
I am not sure where your error comes in...

To illustrate the point, I've created a sample project for you. The ZIP file is attached to this message.

When you extract the file, you will need to replace all underscore characters by periods...

Cheers!

DGV-Demo.zip
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

914 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

21 Experts available now in Live!

Get 1:1 Help Now