Results 1 to 12 of 12

Thread: Sort a listbox

Hybrid View

  1. #1
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,110

    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

  2. #2

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Sort a listbox

    Hey!
    For some how I manage to add data in all cells using a routine. The problem is that I can't add the shole data to DataGridView

    Here is an example

    Code:
    Public Class Form1
    
    'I only have one button and a DataGridView
    
    Dim x As Double
    Dim y As Double
    Dim z As Double
    Dim row As Integer = 4
    Dim col As Integer = 5
    
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    'Generate z number using the FOR LOOP
    
    For Me.x = 1 To 5
    For Me.y = 1 To 4
    z = x * y
    Next y
    Next x
    
    ' GENERATE COLUMNS,ROWS OF DATAGRIDVIEW
    
    DataGridView2.ColumnCount = 5
    DataGridView2.RowCount = 4
    
    
    For Me.col = 0 To 4
     For Me.row = 0 To 3
    DataGridView2.Rows(row).Cells(col).Value = z   '--> HERE IS THE PROBLEM
    Next
    Next
    
    End Sub
    End Class
    In DATA GRID VIEW it is appeared only the last value of z (z=20). I have the represenation I want (see above image) with this kind of code but data are missed. I think that I have to store somewhere the z-values and then add them to DataGridView
    I suppose also that I have some problem with declarations of variables and some other things that I can't figure out.... Unless there is a way to move data from one datagridview to another...
    Any help?

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
  •  



Click Here to Expand Forum to Full Width