Matthew James
Matthew James

Reputation: 11

Data Grid view only displaying one letter in the cell

I am trying to add rows to a datagridview and when loading the from it only displays one letter on the variable I am trying to retrieve.

Dim col As New DataGridViewTextBoxColumn 'adding the colunm player to the data grid view
    col.HeaderText = "Player"
    DataGridView_displayfigures.Columns.Add(col)
    DataGridView_displayfigures.Rows.Add(New String({AssignRuns.batsman1A}))

This is the structure

Structure AssignRuns
    Shared batsman1A As String = PlayerSelection.player1_cmbox.Text
    Shared batsman1Aruns As Integer

I don't understand this. any help would be appreciated

Upvotes: 0

Views: 161

Answers (2)

Julie Xu-MSFT
Julie Xu-MSFT

Reputation: 340

You can try me this way to add rows and columns in the DataGridView control.

Public Class Form1

    Structure AssignRuns
        Shared batsman1A As String = "player1"
        Shared batsman1Aruns As Integer = 3
    End Structure

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'definite  columncount and then add columns of datagridview1
        DataGridView1.ColumnCount = AssignRuns.batsman1Aruns
        DataGridView1.Columns(0).HeaderText = "Player"

        'add rows of datagridview1
        Dim row As String() = {AssignRuns.batsman1A} 'You can try {AssignRuns.batsman1A,AssignRuns.batsman1A,AssignRuns.batsman1A}
        DataGridView1.Rows.Add(row)

    End Sub
End Class

enter image description here

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54457

This is a perfect example of why you should have Option Strict On. You are use a String constructor here:

DataGridView_displayfigures.Rows.Add(New String({AssignRuns.batsman1A}))

But that is definitely not doing what you intend. You are passing a String array as an argument but there is no String constructor that has a parameter of type String array. With Option Strict On, that would generate a syntax error and your code would fail to compile until you fixed it.

With Option Strict Off, the compiler looks for a constructor that is similar enough that it can massage your code to work. In this case, it uses the one that has a parameter of type Char array. That constructor creates a String containing all the Char values from the array.

In your case, each String in your array is converted to a Char by simply taking the first character in each String. Your array only contains one String so only one Char is passed to the constructor, so the String that gets created only contains one character.

As well as that, there's no overload of that Add method that takes a single String either. There is one that takes an Object array though, so that's what you should be passing:

DataGridView_displayfigures.Rows.Add(New Object() {AssignRuns.batsman1A})

This would also work:

DataGridView_displayfigures.Rows.Add({AssignRuns.batsman1A})

That is technically passing a String array to the Add method but that is allowed because it's a widening conversion, i.e. no data can be lost. Converting a String to a Char is a narrowing conversion because data can be lost, as it was in your original code. Option Strict On allows implicit widening conversions but not implicit narrowing conversions.

EDIT:

Actually, thinking about that last code snippet and the fact that you can pass a String array directly to Add makes me think that maybe your mistake was just to misplace a single closing parenthesis because, rather than this:

DataGridView_displayfigures.Rows.Add(New String({AssignRuns.batsman1A}))

you could have had this:

DataGridView_displayfigures.Rows.Add(New String() {AssignRuns.batsman1A})

Moving that closing parenthesis means that you are denoting a String array and then initialising it, rather than passing a literal String array as an argument to a constructor.

Upvotes: 2

Related Questions