I'm newbie in C# programmation and I'm starting creating an application that show me when (real life) "events" expire through ProgressBars.
All info. about these events are stored in a Database; So opening a connection to my-sql database I can retrieve all my events.
The problem is that I don't know how many events are stored in that database (since in a day I can have 20 events and in another I could have other 40 ones) and my Form1 can't resize itself according the number of events.
So, I need to make a page system in order to limit the events printed in each page.
Searching on Google and also on this site, I see that the easiest way to make pages in C# is using DataGridView but I can't use it because I need ProgressBars in order to see when an event expires.
So, I thought to risolve this problem using the Limit data selection of MySQL: SELECT * from EVENTS LIMIT x,y;
where x and y are two variables and they change according several buttons; on button 1 (first page) x and y would be 0 and 20, on button 2 (second page) x and y would be 20,40 and so on..
but this doesn't work since x and y became resetted each time I refresh the Form1.
This is just a piece of code in order to help you to understand my scenario:
public partial class Form1 : Form
{
int x;
private void Form1_Load(object sender, EventArgs e)
{
server = "localhost";
database = "app";
uid = "root";
password = "root";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
if (this.OpenConnection() == true)
{
string query = "SELECT * from EVENTS LIMIT"+ x +","+ y +";";
mySqlDataAdapter = new MySqlDataAdapter(query , connection);
...
...
}
}
private void button1_Click(object sender, EventArgs e)
{
x=0;
y=20;
}
private void button2_Click(object sender, EventArgs e)
{
x=20;
y=40;
}
}
So, I'm looking forward for any idea;
In your button clicks you update the x and y values, but you never re-execute the database query or update the data on the form. You need to fetch the next "page" of data in order to display it.
First, extract your database code into a method rather than in the Load
event:
private void LoadData()
{
server = "localhost";
database = "app";
uid = "root";
password = "root";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
if (this.OpenConnection() == true)
{
string query = "SELECT * from EVENTS LIMIT"+ x +","+ y +";";
mySqlDataAdapter = new MySqlDataAdapter(query , connection);
...
...
}
}
Then in the Load
event set your initial x
and y
values and load the data:
private void Form1_Load(object sender, EventArgs e)
{
x = 0;
y = 20;
LoadData();
}
This should load the initial "page" of data. Then presumably you have a "next" and "previous" button for paging? Each one would increment/decrement the x
and y
values accordingly. Something like this:
private void nextButton_Click(object sender, EventArgs e)
{
x += 20;
y += 20;
LoadData();
}
private void previousButton_Click(object sender, EventArgs e)
{
x -= 20;
y -= 20;
LoadData();
}
Going further, you'll want to add some bounds checking so that people can't click "next" on the last page or "previous" on the first page. Probably some error handling would be a good idea. There are lots of ways to polish this. But the basic idea is that you need to re-query the database and re-bind the form controls with each page.
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
LoadData(0,20);
}
public void LoadData(int x, int y)
{
server = "localhost";
database = "app";
uid = "root";
password = "root";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
if (this.OpenConnection() == true)
{
string query = "SELECT * from EVENTS LIMIT"+ x +","+ y +";";
mySqlDataAdapter = new MySqlDataAdapter(query , connection);
...
...
}
}
private void button1_Click(object sender, EventArgs e)
{
LoadData(0,20);
}
private void button2_Click(object sender, EventArgs e)
{
LoadData(20,40);
}
}
Call LoadData method on every button click event so that data binding takes place.
来源:https://stackoverflow.com/questions/32187039/c-sharp-retrieving-data-from-mysql-and-ordering-them-in-pages-without-datag