-1

I get this error when I added this only code :

table.Columns.Add("Grade", Type.GetType("System.string"))

I have tried to change data type to Char but also was useless what I is to add the "Grade" column and it's value like (A+,B,..)

Imports System.Data.DataTable

Public Class Form1
    Dim table As New DataTable("Table")
    Dim index As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        table.Columns.Add("ID", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))
        table.Columns.Add("Programming", Type.GetType("System.Int32"))
        table.Columns.Add("Law", Type.GetType("System.Int32"))
        table.Columns.Add("English", Type.GetType("System.Int32"))
        table.Columns.Add("GPA", Type.GetType("System.Int32"))
        table.Columns.Add("Grade", Type.GetType("System.string"))

        table.Rows.Add(1, "XXXX", "YYYYY", 21, 88, 77, 90, 89)
        table.Rows.Add(2, "SSDD", "hGSQ", 33, 70, 96, 72, 82)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
        table.Rows.Add(7, "aefht", "cvfhytrff", 21)
        table.Rows.Add(8, "wghyuj", "mihgdwrh", 33)
        table.Rows.Add(9, "qsztii", "bvdhjh", 53)
        table.Rows.Add(10, "rytyufd", "esdfzr", 19)

        DataGridView1.DataSource = table
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Table.rows.Add(TextBoxID.Text, TextBoxFN.Text, TextBoxLN.Text, TextBoxAGE.Text)
        DataGridView1.DataSource = table
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBoxAGE.TextChanged

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick, DataGridView1.CellClick
        index = e.RowIndex
        Dim selectedRow As DataGridViewRow
        selectedRow = DataGridView1.Rows(index)
        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()
        TextBoxPro.Text = selectedRow.Cells(4).Value.ToString()
        TextBoxLaw.Text = selectedRow.Cells(5).Value.ToString()
        TextBoxENGL.Text = selectedRow.Cells(6).Value.ToString()
        TextBoxGPA.Text = selectedRow.Cells(7).Value.ToString()
        TextBoxGrade.Text = selectedRow.Cells(8).Value.ToString()
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Dim newDataRow As DataGridViewRow

        newDataRow = DataGridView1.Rows(index)

        newDataRow.Cells(0).Value = TextBoxID.Text
        newDataRow.Cells(1).Value = TextBoxFN.Text
        newDataRow.Cells(2).Value = TextBoxLN.Text
        newDataRow.Cells(3).Value = TextBoxAGE.Text
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        DataGridView1.Rows.RemoveAt(index)
    End Sub

    Private Sub TextBoxID_TextChanged(sender As Object, e As EventArgs) Handles TextBoxID.TextChanged

    End Sub

    Private Sub TextBoxFN_TextChanged(sender As Object, e As EventArgs) Handles TextBoxFN.TextChanged

    End Sub
End Class
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
BDBD
  • 1
  • 1
    I would guess it's because you used "System.string" rather than "System.String". – jmcilhinney Apr 04 '19 at 09:57
  • 1
    You shouldn't be using the `Type.GetType` method in that context to begin with. Only use that when the type name comes from elsewhere via a variable. When you're hard-coding, use the `GetType` operator, e.g. `table.Columns.Add("Grade", GetType(String))`. Because you are providing a data type rather than the name of a type, you get compile-time checking rather than run-time checking. – jmcilhinney Apr 04 '19 at 10:03
  • it worked now thanks but for it's value i can add Like(A,B,F) but how can i add A+, C+ – BDBD Apr 04 '19 at 10:45

1 Answers1

0

Based on @jmcilhinney's comments:

  • The type name passed to Type.GetType() is case sensitive. Type.GetType("System.String") will work correctly.
  • Avoid using Type.GetType() if it is not necessary. Use the GetType() operator instead: GetType(String) instead of Type.GetType("System.String"). That way the compiler can check your code more effectively. (The compiler typically does not check for valid string contents.)

So your Form_Load event handler would become something like:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    table.Columns.Add("ID", GetType(Integer))
    table.Columns.Add("First Name", GetType(String))
    table.Columns.Add("Last Name", GetType(String))
    table.Columns.Add("Age", GetType(Integer))
    table.Columns.Add("Programming", GetType(Integer))
    table.Columns.Add("Law", GetType(Integer))
    table.Columns.Add("English", GetType(Integer))
    table.Columns.Add("GPA", GetType(Integer))
    table.Columns.Add("Grade", GetType(String))





    table.Rows.Add(1, "XXXX", "YYYYY", 21, 88, 77, 90, 89)
    table.Rows.Add(2, "SSDD", "hGSQ", 33, 70, 96, 72, 82)
    table.Rows.Add(3, "fgfgd", "jgfdd", 53)
    table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
    table.Rows.Add(5, "hghfd", "ghjgdf", 36)
    table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
    table.Rows.Add(7, "aefht", "cvfhytrff", 21)
    table.Rows.Add(8, "wghyuj", "mihgdwrh", 33)
    table.Rows.Add(9, "qsztii", "bvdhjh", 53)
    table.Rows.Add(10, "rytyufd", "esdfzr", 19)


    DataGridView1.DataSource = table


End Sub

(Based on the code you supplied, I have no clue where your table variable is declared, but I assume everything will be correct this way.)

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22