-1

I have the following code and I want to bind it to a DataGridView.

Public Class Form1
    Private _myTable As New DataTable()
    Private Sub LoadData()
        Dim item As New List(Of item())
        Grid.Bind(item)
    End Sub
    Private Sub FillDataTable(iRow As Integer, ByVal Codeproduct As String)
 _myTable.Columns.Add("No", GetType(Integer))
        _myTable.Columns.Add("Codeproduct", GetType(String))
        Dim row As DataRow = _myTable.NewRow()
        row("No") = iRow
        row("Codeproduct") = Codeproduct
        _myTable.Rows.Add(row)
    End Sub
    Private Sub AddColumnsProgrammatically()
        Dim Col1 = New DataGridViewTextBoxColumn()
        Dim Col2 = New DataGridViewTextBoxColumn()
        Col1.HeaderText = "No"
        Col1.Name = "Column1"
        Col1.DataPropertyName = "No"
        Col2.HeaderText = "CodeProduct"
        Col2.Name = "Column2"
        Col2.DataPropertyName = "CodeProduct"
        Grid.Columns.AddRange(New DataGridViewColumn() {Col1, Col2})
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddColumnsProgrammatically()
        LoadData()
    End Sub
    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim iRow As Integer
        If Grid.RowCount - 1 = 0 Then
            iRow = 1
        Else
            iRow = Convert.ToInt32(Grid.Rows(Grid.RowCount - 2).Cells(0).Value.ToString()) + 1
        End If
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            FillDataTable(iRow, TextBox1.Text)
            Grid.DataSource = _myTable
            TextBox1.Clear()
        End If
    End Sub
End Class
Public Class DataControl
    Public Shared Function CreateDataTableDynamic(ByVal field() As String) As DataTable
        Dim i As Integer
        Dim dtTable As New DataTable("MyTable")
        For i = 0 To field.GetUpperBound(0)
            dtTable.Columns.Add(New DataColumn(field(i)))
        Next i
        Return dtTable
    End Function
End Class
Public Class item
    Public Property No() As Integer
    Public Property CodeProduct() As String
End Class

I want to ask if there is anything wrong with my code, and how can I bind my DataGridView to this data properly.

after add code "Columns.Add" and Fill the textbox in the second fill to DataGridView but still error

Fill the textbox in the second fill to DataGridView but still error

error when Fill the textbox in the second to DataGridView

[error when Fill the textbox in the second to DataGridView breakpoint with diagnostic tool

[breakpoint with diagnostic tool

roy
  • 693
  • 2
  • 11
  • Did you bother to look at the columns in the `DataTable` to see what is there? I see a method that adds columns to a `DataTable` but I don't see where you're calling it so I have no idea what the names of those columns are. Clearly none of them are name "No" or you wouldn't be getting that error message. – jmcilhinney Jun 17 '23 at 04:05
  • @jmcilhinney , `I see a method that adds columns to a DataTable but I don't see where you're calling it so I have no idea what the names of those columns are. ` `myTable.Columns.Add("No", GetType(Integer)) _myTable.Columns.Add("Codeproduct", GetType(String))` just now I updated the code you mean but there is still an error A column named 'No' already belongs to this DataTable. if I do textbox filling a second time – roy Jun 17 '23 at 04:34
  • @jmcilhinney , ` Clearly none of them are name "No" or you wouldn't be getting that error message.` You mean I have to rename the "No" column – roy Jun 17 '23 at 04:37
  • Why are you adding columns to the `DataTable` in the method that adds a row? That means that, if you add two rows, you'll be adding the same columns twice, which doesn't make sense. Add the columns once, as part of the initialisation. You need to debug your code properly. If you don't know how to do that, stop what you're doing and learn that first. – jmcilhinney Jun 17 '23 at 05:01
  • @jmcilhinney , `Why are you adding columns to the DataTable in the method that adds a row? That means that, if you add two rows, you'll be adding the same columns twice, which doesn't make sense.` if I don't do Columns.Add Then an error appears "Column does not belong the table" then if I do colum.add in the add row method Then an error appears if I fill the textbox twice into the datagridview "A column named [column name] already belongs to this DataTable" – roy Jun 17 '23 at 06:04
  • @jmcilhinney , `You need to debug your code properly. If you don't know how to do that, stop what you're doing and learn that first.` I've done it with breakpoint with diagnostic tool and I have posted screenshots – roy Jun 17 '23 at 06:11
  • You've changed your code and changed your question to a completely different issue but it should still be obvious, especially since I told you what the issue is. Every time you want to add a row, you add the columns. If you actually debugged properly then you would be seeing that. If you try to add columns with the same names multiple times, why would you be surprised that columns with the same names already exist? I told you what you need to do but I'll say it again: add the columns once as part of the initialisation, not every time you add a row. Think about the logic first. – jmcilhinney Jun 17 '23 at 06:16
  • @jmcilhinney , Thank you for your guidance. I've put columns.add in the load form and no more problems. Can you please provide it as an answer so that I can mark the solution of your answer – roy Jun 17 '23 at 06:35
  • 1
    Feel free to add your own answer. – jmcilhinney Jun 17 '23 at 07:02
  • @jmcilhinney , Thank you for your guidance – roy Jun 17 '23 at 07:14

1 Answers1

0

In accordance with the guidelines of @jmcilhinney

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_myTable.Columns.Add("No", GetType(Integer))
_myTable.Columns.Add("Codeproduct", GetType(String))
        AddColumnsProgrammatically()
        LoadData()
    End Sub
roy
  • 693
  • 2
  • 11