Solved

How do I display a two-dimensional array?

Posted on 2009-07-14
6
161 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

860 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