Solved

How do I display a two-dimensional array?

Posted on 2009-07-14
6
157 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Accepted Solution

by:
NotLogical earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

11 Experts available now in Live!

Get 1:1 Help Now