Results 1 to 10 of 10

Thread: [RESOLVED] DataGridView output not showing?

  1. #1

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Resolved [RESOLVED] DataGridView output not showing?

    For a class project I'm supposed to write a program that requests a team as input and displays the players from that team in the first column. The players should be sorted alphabetically by their last name, and if they have the same name they should be sorted by first name accordingly. My program compiles and I'm fairly certain that I've done most of it correctly. The second column should be filled with the batting average of the corresponding player from the left column.

    My problems are as follows:

    1) I can't get the DGV output to show correctly, it posts the player's name with the batting average of that player in the following downward cell.

    2) I'm not sure how to further use the .Split method to separate the first and last name after already separating the name from team/at bats/hits, and can't find any info regarding this topic. Also, I can't figure out how to sort in reverse, I've only seen the Ascending and Descending options so far for sorting.

    3) Is the way I've figured the batting average the most efficient/correct way, or is there another better way?

    Thanks in advance to anyone that helps, it's greatly appreciated by a knowledge-hungry newcomer!



    Code:
    Public Class danbrockplayers
        Structure player
            Dim name, team, atBats, hits, average
        End Structure
    
        Dim players() As player
    
        Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
            gridStats.Rows.Clear()
    
            Dim query = From player In players
                        Where player.team = txtTeam.Text
                        Order By player.name
                        Select player.name, player.average
    
            For Each player In query
                gridStats.Rows.Add(player.name)
                gridStats.Rows.Add(player.average)
            Next
    
        End Sub
    
        Private Sub danbrockplayers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Place the data for each player into the array players
    
            Dim allPlayers() = IO.File.ReadAllLines("Baseball.txt")
            Dim n = allPlayers.Count - 1
            ReDim players(n)
            Dim line As String  'holds data for a single player
            Dim data() As String
    
            For i As Integer = 0 To n
                line = allPlayers(i)
                data = line.Split(","c)
                players(i).name = data(0)
                players(i).team = data(1)
                players(i).atBats = CInt(data(2))
                players(i).hits = CInt(data(3))
                players(i).average = players(i).hits / players(i).atBats
            Next
        End Sub
    End Class
    Attached Files Attached Files

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataGridView output not showing?

    1/ instead of this:

    vb Code:
    1. For Each player In query
    2.     gridStats.Rows.Add(player.name)
    3.     gridStats.Rows.Add(player.average)
    4. Next

    try this:

    vb Code:
    1. For Each player In query
    2.     gridStats.Rows.Add(new Object(){player.name, player.average})
    3. Next

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataGridView output not showing?

    2/ i didn't read the text file right through, but it looks like you can split first + last name on " "c

  4. #4

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Re: DataGridView output not showing?

    Quote Originally Posted by .paul. View Post
    1/ instead of this:

    vb Code:
    1. For Each player In query
    2.     gridStats.Rows.Add(player.name)
    3.     gridStats.Rows.Add(player.average)
    4. Next

    try this:

    vb Code:
    1. For Each player In query
    2.     gridStats.Rows.Add(new Object(){player.name, player.average})
    3. Next
    BAD.ASS.

    Thanks dude, I've been googling around for like 3 hours trying to figure this part out on my own! I'll try your advice for my second problem when I get back from dinner.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataGridView output not showing?

    the way you were adding, you were adding 2 items to 2 rows, instead of 2 items to 1 row.
    every time you call dgv.Rows.Add it adds a new row.

  6. #6

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Re: DataGridView output not showing?

    Quote Originally Posted by .paul. View Post
    the way you were adding, you were adding 2 items to 2 rows, instead of 2 items to 1 row.
    every time you call dgv.Rows.Add it adds a new row.
    I don't know why this didn't dawn on me earlier, it seems self evident to me now though, haha.

    As far as using the split method goes, I understand that I can use a space character as delimiter .split(" "c), but what should I do to use that in the DGV? Should I edit my structure to include 'firstName' and 'lastName' instead of just 'name'?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataGridView output not showing?

    add firstName + lastName members to your player structure + when querying:

    vb Code:
    1. Order By lastName, firstName _

    you can continue just displaying the name + average fields in your dgv, + they'll be sorted as specified

  8. #8

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Re: DataGridView output not showing?

    Quote Originally Posted by .paul. View Post
    add firstName + lastName members to your player structure + when querying:

    vb Code:
    1. Order By lastName, firstName _

    you can continue just displaying the name + average fields in your dgv, + they'll be sorted as specified
    So I can use multiple fields to sort specifically for cases like this where 2 instances of the same field might be identical?

    I'm not sure if this is a dumb question, but can I use split like this:
    Code:
    line.split(" ",","c)
    to split using space character as delimiter and also use a comma as a delimiter?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [RESOLVED] DataGridView output not showing?

    no split by ","c first as you originally were. then split name member by " "c to get first + last names

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [RESOLVED] DataGridView output not showing?

    last recommendation.
    try turning option strict on + fixing the errors it throws up (not my errors)

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