private void BuildList()
{
// Define initial variables
int Limit = Int32.Parse(LimitSelect.SelectedItem.ToString());
int Start = (ListPage == 1) ? 0 : (ListPage - 1) * Limit;
string Like = SearchBox.Text.Replace("'", "").Trim();
WhereClause Where = null;
// Build Query
using (GamespyDatabase Driver = new GamespyDatabase())
{
SelectQueryBuilder Query = new SelectQueryBuilder(Driver);
Query.SelectColumns("id", "name", "email", "country", "lastip", "session");
Query.SelectFromTable("accounts");
Query.AddOrderBy(SortedCol.Name, ((SortDir == ListSortDirection.Ascending) ? Sorting.Ascending : Sorting.Descending));
Query.Limit(Limit, Start);
// User entered search
if (!String.IsNullOrWhiteSpace(Like))
Where = Query.AddWhere("name", Comparison.Like, "%" + Like + "%");
// Online Accounts
if (OnlineAccountsCheckBox.Checked)
{
if (Where == null)
Where = Query.AddWhere("session", Comparison.NotEqualTo, 0);
else
Where.AddClause(LogicOperator.And, "session", Comparison.NotEqualTo, 0);
}
// Clear out old junk
DataTable.Rows.Clear();
// Add players to data grid
int RowCount = 0;
foreach (Dictionary<string, object> Row in Driver.QueryReader(Query.BuildCommand()))
{
DataTable.Rows.Add(new string[] {
Row["id"].ToString(),
Row["name"].ToString(),
Row["email"].ToString(),
Row["country"].ToString(),
(Row["session"].ToString() != "0") ? "Yes" : "No",
//(Gamespy.GamespyServer.IsPlayerConnected(Int32.Parse(Row["id"].ToString())) ? "Yes" : "No"),
Row["lastip"].ToString(),
});
RowCount++;
}
// Get Filtered Rows
Query = new SelectQueryBuilder(Driver);
Query.SelectCount();
Query.SelectFromTable("accounts");
if (Where != null) Query.AddWhere(Where);
int TotalFilteredRows = Query.ExecuteScalar<int>();
// Get Total Player Count, if the Where clause is null, this will be the same as the Filtered Row Count
int TotalRows = TotalFilteredRows;
if (Where != null)
{
Query = new SelectQueryBuilder(Driver);
Query.SelectCount();
Query.SelectFromTable("accounts");
TotalRows = Query.ExecuteScalar<int>();
}
// Stop Count
int Stop = (ListPage == 1) ? RowCount : ((ListPage - 1) * Limit) + RowCount;
// First / Previous button
if (ListPage == 1)
{
FirstBtn.Enabled = false;
PreviousBtn.Enabled = false;
}
else
{
FirstBtn.Enabled = true;
PreviousBtn.Enabled = true;
}
// Next / Last Button
LastBtn.Enabled = false;
NextBtn.Enabled = false;
// Get total number of pages
if (TotalFilteredRows / (ListPage * Limit) > 0)
{
float total = float.Parse(TotalFilteredRows.ToString()) / float.Parse(Limit.ToString());
TotalPages = Int32.Parse(Math.Floor(total).ToString());
if (TotalFilteredRows % Limit != 0)
TotalPages++;
LastBtn.Enabled = true;
NextBtn.Enabled = true;
}
// Set page number
PageNumber.Maximum = TotalPages;
PageNumber.Value = ListPage;
// Update Row Count Information
RowCountDesc.Text = String.Format(
"Showing {0} to {1} of {2} account{3}",
++Start,
Stop,
TotalFilteredRows,
((TotalFilteredRows > 1) ? "s " : " ")
);
// Add Total row count
if (!String.IsNullOrWhiteSpace(Like))
RowCountDesc.Text += String.Format("(filtered from " + TotalRows + " total account{0})", ((TotalRows > 1) ? "s" : ""));
// Update
DataTable.Update();
}
}