简体   繁体   中英

Button to Load more data to DatagridView, more than 20000 register C#

I have a Big doudt.

I'm trying to load my data with this code:

        SqlConnection con = new SqlConnection(Login.conectData);
        con.Open();
        DataSet dsFabricantes = new DataSet();
        SqlDataAdapter daFabricantes = new SqlDataAdapter("SELECT TOP 100 * FROM 
        Fabricantes", con);
        dsFabricantes.Clear();

        //prencher a tabela
        daFabricantes.Fill(dsFabricantes, "Fabricantes");
        tabelaFabricantes.DataSource = dsFabricantes;
        tabelaFabricantes.DataMember = "Fabricantes";


        //para mudar o que está escrito no cabeçalho das colunas

        tabelaFabricantes.Columns[1].HeaderCell.Value = "Nome do 
         Fabricante";
        tabelaFabricantes.Columns[2].HeaderCell.Value = "Observações";

        con.Close();
        tabelaFabricantes.ClearSelection();

I Want to make a button to load more register in my datagridView. Onload of the app I am changing just 100 register. My question is:

How can I add a button to onClickbutton this button, my dataGrideView, Load more 100 Register and so on and so on ?

little Help thank you Bráulio José

You'll need to dynamically adapt your SqlDataAdapter() call.

First set some global variables on page/formOnLoad method

global int startRow = '1';
global int maxColRank = testMaxRows();


#set left button active = false
leftButton.Active = false;

To do this, class your DataGridViewLoader separately to accept no parameters. Name the class mySqlGridViewLoaderClass() at the beginning of this class input these values:

string MaximumRows = '100';
string StartRowIndex = StartRow.ToString();

In the SQL connector for this DataGridViewLoader, use a query similar to

SELECT colRank, col1, col2
FROM
   (SELECT col1, col2,
       ROW_NUMBER() OVER(ORDER BY col2 DESC) AS colRank
    FROM Fabricantes
   ) AS rowNumQuery
WHERE colRank > <i>StartRowIndex</i> AND
      colRank <= (<i>StartRowIndex</i> + <i>MaximumRows</i>
   )

Your class should perform DataGridView loading operation, then return a second scalar value for MAX(colRank).

#new connector
SqlDataAdapter daFabricantes = new SqlDataAdapter("SELECT Max(colRank) FROM 
    (SELECT max(colRank)
FROM
   (SELECT col1, col2,
       ROW_NUMBER() OVER(ORDER BY col2 DESC) AS colRank
    FROM Fabricantes
   ) AS rowNumQuery
WHERE colRank > <i>StartRowIndex</i> AND
      colRank <= (<i>StartRowIndex</i> + <i>MaximumRows</i>
   ))", con2);
int mr = (Int32) con2.ExecuteScalar();
return mr;

class a second SQL connector loader to test max Rows and return a scalar value. name the class testMaxRows() In the sql connector, use a query similar to:

 Select count(col1) DIST from fabricantes;

Return the count to the tr variable. The last 2 lines of maxRows class should be:

int tr = (Int32) con.ExecuteScalar();
return tr;

In your form designer, create a button for '<' and '>' Set < button to inactive. > button to active

In the '<' ButtonOnClick() event:

rightButton.Active = true;
if (StartRow != '1') 
{
     StartRow -= 100;

     # your class expects a return value, so we'll initialize one
     int mr = (Int32) mySqlGridViewLoaderClass();

     if (StartRow == 1) 
     {
         leftButton.Active = false;
     }
}

In the '>' ButtonOnClick() event:

leftButton.Active = true;

StartRow += 100;
int maxRank = (Int32) mySqlGridViewLoaderClass();

if ( maxRank >= maxColRank)
{
    rightButton.Active = false;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM