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

x
?
Solved

How do I display a two-dimensional array?

Posted on 2009-07-14
6
Medium Priority
?
165 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Accepted Solution

by:
NotLogical earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

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