How to associate datatable fields to Datagridview columns?

Asked

Viewed 1,867 times

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 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:

  • 1º Performs Database Search and Returns Completed Datatable
  • 2º Creates columns in datagridview and associates them to 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 consultation, I have the field "Product code"... but the program triggers an error when arriving at the part where it gives a name to Datagridview column 0.

Error : "Unable to set the Columncount property in a data-limited Datagridview control."

1 answer

1


This form will cause an error in the next query, so in the input it will work after that it will give an error because the columns are already present ... then do the separate configuration routine, the update ...

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, so you can do the searches and return this Datatable the way you want (with filter for example). The Button ButIniciar_Click, only executes the method Executar_Pesquisar that it only assigns Datatable (The Datatable that is in the code is an example).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.