1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Rep Power

    Cool Sorting column in datagridview populated by array

    Below is my code. I need to sort the output of the "z" column descending by value. Any ideas?

    Private Sub btnTableDeg_Click(sender As Object, e As EventArgs) Handles btnTableDeg.Click

    ' Clears rows on button press and builds columns
    DataGridView1.ColumnCount = 3

    DataGridView1.Columns(0).HeaderText = "Field of Study"
    DataGridView1.Columns(1).HeaderText = "1981"
    DataGridView1.Columns(2).HeaderText = "2006"

    ' One dimensional arrays initializing values
    Dim x() As String = {("Business"), ("Computer & info.science"), ("Education"), ("Engineering"), ("Social science and history")}
    Dim y() As Integer = {("200,521"), ("15,121"), ("108,074"), ("63,642"), ("100,513")}
    Dim z() As Integer = {("318,042"), ("47,480"), ("107,238"), ("67,045"), ("161,485")}

    'Calling output from arrays for datagridview
    DataGridView1.Rows.Add(x(0), (y(0)), z(0))
    DataGridView1.Rows.Add(x(1), (y(1)), z(1))
    DataGridView1.Rows.Add(x(2), (y(2)), z(2))
    DataGridView1.Rows.Add(x(3), (y(3)), z(3))
    DataGridView1.Rows.Add(x(4), (y(4)), z(4))

    End Sub
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Rep Power
    I presume that the data you have provided is sample data only, and that the real data will come from another and larger source. The simplest and most flexible way is to load the data into a database, and then bind your DataGrid to the database. That way you can sort the data any way you want to simply by changing the record source and refreshing the grid. For example, you can have the program respond to a click on any of the column headers to sort the records according to that particular column.

    J.A. Coutts

IMN logo majestic logo threadwatch logo seochat tools logo