Solved

How do I display a two-dimensional array?

Posted on 2009-07-14
6
164 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 13 hours left to enroll

623 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