|
-
May 6th, 2014, 05:03 PM
#10
Re: Sort a listbox
There is a general convention in programs that tabular data has fields as the columns, and each row is a set of values for these fields. In other words, data is generally added by row. You have seen this in the Listview and the DGV, as both of them are really designed primarily for this kind of mapping. With the DGV, you can add data to individual cells, so you can certainly add by column or by row, though the DGV makes it more natural to add by row rather than by column.
In your case, it may make more sense to you to add the data column by column, but you don't have to do it that way from what I can see. It looks like you know the number of columns that will be in the final grid before you start putting any data in the grid. Therefore, you can add the number of columns you need, and the number of rows you need, then populate them by row, by column, or by darn near random. The issue isn't with the grid or the means to access it, both of which you have. The issue is with the For...Next loop you are using to populate the grid.
A loop of that nature is probably going to run from 0 to N, or 1 to N. This is a single value, though. The problem is to translate that single value into a row, column in the DGV. That's not difficult, though it might be a bit new to you. The key is whether you are going to go by columns or by rows. Assuming you are going to fill column 1 first, then column 2, then column 3, and asssuming that the picture is correct, the basic design is:
The first 9 items go into column 1 row 1 - 9
The next 9 items go into column 2 row 1 - 9
etc.
Assuming that the loop begins with 0, the loop would look something like this:
Code:
For x = 0 to 53
Dim row As Integer = x Mod 9
Dim col As Integer = x \ 9 'Note that this is integer division, not the more common /.
yourDGV.Rows(row).Cells(col).value = whatever
Next
You can do that in one line, too, if you don't want the row and col variables, but I felt those might be more clear.
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|