Pageing a Datagridview



  • Good afternoon. I have a datagridview that carries several lines, I have been researching on the net how I can paginate this datagridview, but I couldn't perform such procedure, because I didn't have so much experience in c#, so here I once again asked for the help of the forum to get to page the datagridview, i.e. cirating pages the datagridview.

    Follow my screen inserir a descrição da imagem aqui

    follows the code that loads the datagrid

    private void ListaGrid()
            {
                try
                {
                    SqlCommand tabnet = new SqlCommand("usp_TabelaPrecosNet", conexaoDADOADV(true));
                    tabnet.Parameters.AddWithValue("@CODTABELA", this.cb_tabela.Text);
                    tabnet.CommandType = CommandType.StoredProcedure;
                    tabnet.ExecuteNonQuery();
    
                SqlDataAdapter dados = new SqlDataAdapter(tabnet);
                DataTable dtLista = new DataTable();
                dados.Fill(dtLista);
    
                dgw_listanet.DataSource = dtLista;
    
                dgw_listanet.Columns["CODPRODUTO"].HeaderText = "COD.PRODUTO";
                dgw_listanet.Columns["DESCRICAO"].HeaderText = "PROD. DESCRIÇÃO";
                dgw_listanet.Columns["IPI"].HeaderText = "% IPI";
                dgw_listanet.Columns["PRECO"].HeaderText = "PREÇO NET";
                dgw_listanet.Columns["MOEDA"].HeaderText = "TIPO MOEDA";
    
            }
            catch
            {
                MessageBox.Show("Não exstem dados digitados para a consulta, por favor verificar!!!");
                return;
            }
        }
    



  • First you should change your procedure to return the paginated values by spending 2 extra stops.

    -Index of the page; - How much records requested.

    CREATE PROCEDURE dbo.proc_PaginadaExemplo
    (
    @IndexPagina int,
    @QuantidadeRegistros int
    )
    AS
    

    SET NOCOUNT ON

    DECLARE @PrimeiroRegistro int, @UltimoRegistro int

    SELECT @PrimeiroRegistro = (@IndexPagina - 1) * @UltimoRegistro
    SELECT @UltimoRegistro = (@IndexPagina * @QuantidadeRegistros + 1);

    WITH ResultadoTemp as
    (
    SELECT ROW_NUMBER() OVER(ORDER BY <CampoOrdenacao> DESC) as NumeroLinha,
    s.<campo1>, m.<campo2>, s.<campo3>, l.<campo4>
    FROM dbo.<tabela> m
    INNER JOIN dbo.<tabela2> s
    ON s.<campo1> = m.<campo2>
    )
    SELECT top (@UltimoRegistro-1) *
    FROM ResultadoTemp
    WHERE NumeroLinha > @PrimeiroRegistro
    AND NumeroLinha < @UltimoRegistro

    SET NOCOUNT OFF
    GO

    And on your screen must contain fields and buttons for pagination control and number of records.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 1
  • 2
  • 2
  • 2
  • 3
  • 3
  • 2