Paging a Datagridview

Asked

Viewed 183 times

2

Good afternoon to you... I have a datagridview that loads several lines, I’ve been searching on the net how I can paginate this datagridview, but I could not perform such procedure, because I have not so much experience in c#, so I would ask here again for the help of the forum to be able to paginate the datagridview, or to create pages in 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;
            }
        }

1 answer

2

First you must change your program to return the paged values by passing 2 more parameters.

-index of the page; -Number of requested records.

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 should contain fields and buttons for pagination control and number of records.

  • Leandro good afternoon and thanks for your attention, now let me see if I understand, I need to put all this query inside my porcedure and this or I have to create a new Process.

  • You must implement these parameters in your query. In short, change the select from the example to your query in the process.

Browser other questions tagged

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