How do I associate datatable fields with DataGridView columns?

3

My problem is following, I make a query in the database that returns me a datatable with a wide variety of fields. The goal is to display this query in the datagridview, however, displaying only some of the datatable fields.

I had a previous code here, but it just stopped working.

I will describe the process:

  • 1st Perform Bank Search and Return Datatable Filled
  • 2º Create columns in the datagridview and associate them with the Datatable fields

    With DgvErros
        .ColumnCount = 2
        .AutoGenerateColumns = False
        .Columns(0).Name = "Codigo_Produto"
        .Columns(0).DataPropertyName = "Codigo_Produto"
        .Columns(1).Name = "Descricao"
        .Columns(1).DataPropertyName = "Descricao"
        .DataSource = Bs
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    End With
    

In my query, I have the field " Product_Code " ... but the program throws an error when it arrives at the part where it gives a column 0 to the DataGridView.

Error: "Can not set ColumnCount property in a data-bound DataGridView control."

    
asked by anonymous 14.04.2014 / 17:03

1 answer

1

This form will cause an error in the next search, so in the entry it will work fine after this it will give an error because the columns are already present ... then, do the separate configuration routine, the update routine .. .

Example:

Private Sub Configurar_GridView()
    With DgvErros
        .ColumnCount = 2
        .AutoGenerateColumns = False
        .Columns(0).Name = "Codigo_Produto"
        .Columns(0).DataPropertyName = "Codigo_Produto"
        .Columns(1).Name = "Descricao"
        .Columns(1).DataPropertyName = "Descricao"
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        .AllowUserToAddRows = False
        .AllowUserToDeleteRows = False
        .AllowUserToOrderColumns = False
        .AllowUserToResizeColumns = False
        .AllowUserToResizeRows = False
    End With
End Sub

Private Sub Executar_Pesquisa()
    Dim Bs = New DataTable("Bs")
    Bs.Columns.Add("Codigo_Produto")
    Bs.Columns.Add("Descricao")
    Dim Row = Bs.NewRow()
    Row("Codigo_Produto") = 1
    Row("Descricao") = "Nome 1"

    Bs.Rows.Add(Row)

    'Dim DgvErros = New DataGridView
    With DgvErros
        .DataSource = Bs
    End With
    Panel1.Controls.Add(DgvErros)
End Sub

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

Private Sub ButIniciar_Click(sender As Object, e As EventArgs) Handles ButIniciar.Click        
    Executar_Pesquisa()
End Sub

Because the configuration is only 1 time, then you can do the searches and return this DataTable the way you want (with filter for example). The ButIniciar_Click button only executes the Executar_Pesquisar method that the same only assigns the DataTable (The DataTable that is in the code is an example).

    
14.04.2014 / 17:30