'How make pages in Winforms?
I would like to make pages for UserControl but I don't know how I can do this.
How can I implement this as in the picture in WinForms?
I would like to display as many cards on the page as indicated in the filter, but I don't know how to transfer the remaining cards to the next page:
My code
private void button1_Click(object sender, EventArgs e)
{
flowLayout.Controls.Clear();
MySqlConnection sqlConnection = new MySqlConnection(connectionString);
sqlConnection.Open();
MySqlCommand cmd = new MySqlCommand(query, sqlConnection);
try
{
using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
ShopItem[] listItems = new ShopItem[dt.Rows.Count];
for (int i = 0; i < 1; i++)
{
foreach (DataRow row in dt.Rows)
{
listItems[i] = new ShopItem();
listItems[i].id = Convert.ToInt32(row["id_order"]);
listItems[i].brand = row["brand"].ToString();
listItems[i].capacity = row["capacity"].ToString();
listItems[i].engine = row["engine"].ToString();
listItems[i].lifting_height = row["lifting_height"].ToString();
listItems[i].mass = row["mass"].ToString();
MemoryStream ms = new MemoryStream((byte[])row["image"]);
Bitmap bitmap = new Bitmap(ms);
listItems[i]._image = bitmap;
listItems[i].price = row["price"].ToString();
//if select first value display one item
if (comboBoxFilter.SelectedIndex == 0)
{
if(flowLayout.Controls.Count < Convert.ToInt32(comboBoxFilter.SelectedItem))
{
flowLayout.Controls.Add(listItems[i]);
}
}
//if select second value display four item
if (comboBoxFilter.SelectedIndex == 1)
{
if (flowLayout.Controls.Count < Convert.ToInt32(comboBoxFilter.SelectedItem))
{
flowLayout.Controls.Add(listItems[i]);
}
}
//if select third value display nine item
if (comboBoxFilter.SelectedIndex == 2)
{
if (flowLayout.Controls.Count < Convert.ToInt32(comboBoxFilter.SelectedItem))
{
flowLayout.Controls.Add(listItems[i]);
}
}
}
}
Solution 1:[1]
You assuming that you have a pageSize
and a page
value you can get the corresponding chunk from Rows
as follows:
for (int myIndex = page * pageSize; myIndex < (page + 1) * pageSize; myIndex++) {
//Do some stuff with dt.Rows[i]
}
However, it would be better to not even get all the rows from the database, you could modify your query
to limit
your results, which you can do in any RDBMS, but the syntax varied. If you would not even get all the rows from the database, then you can loop with row
as you originally did, because the paging was already done by the query. However, if you do really want to apply the paging logic on your application server, as your question's tags suggest, then you can do the looping as suggested above.
In general, it is much better to limit the results on the RDBMS's side, so you do not fill the memory upon each request with n * page rows while you only need n x less rows. So, if you do not have a very good reason to load all the rows, then do the limitation in the query. This is especially helpful if you have big data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | marc_s |