using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace DotSpatialTest1
{
public partial class Form2 : Form
{
int row = 0; //每页显示行数
int currentPage = 0; //当前页号
int rowCount = 0; //当前记录行
int sum = 0; //总记录数
int pageCount = 0; //页数=总记录数/每页显示行数
public Form2()
{
InitializeComponent();
}
DataTable table = new DataTable();
DBHelp DB = new DBHelp();
private void Form2_Load(object sender, EventArgs e)
{
// string strsql = "select * from ( select row_.*, rownum rownum_ from ( select *from users ) row_ where 10 >= rownum ) where rownum_ >= 1";
string strsql = "SELECT ROWNUM,ID,USERNAME,ALIASNAME,CREATEDATE,EXPIRETIME FROM USERS WHERE ENABLE=0";
DataSet ds = DB.returnDataSet(strsql);
// this.dw_sql.DataSource = ds.Tables[0];
table = ds.Tables[0]; //传入datatable
InitDataSet();
LoadData();
/*
bs.DataSource = ds.Tables[0];
bindingNavigator1.BindingSource = bs;
dw_sql.DataSource = bs;
*/
DB.CloseConnection();
}
BindingSource bs = new BindingSource();
private void InitDataSet()
{
if (tsb_showPageSize.SelectedItem.ToString().Equals("全部"))
{
row = 100; //设置页面行数
}
else
{
row = Convert.ToInt32(tsb_showPageSize.SelectedItem.ToString()); //设置页面行数
}
sum =table.Rows.Count; //总行数
pageCount = (sum/row); //计算出总页数
if ((sum % row) > 0) pageCount++;
currentPage = 1; //当前页数从1开始
rowCount = 0; //当前记录数从0开始
}
//末页
private void EndDataSetPage()
{
row = Convert.ToInt32(tsb_showPageSize.SelectedItem.ToString()); //设置页面行数
sum = table.Rows.Count;
pageCount = (sum / row); //计算出总页数
int rou = sum % row; //取数
if (rou > 0)
{
rowCount = sum - (sum % row);//不是正好整除页码,取余数页
pageCount++;
}
else
{
rowCount = sum - row;
}
currentPage = pageCount; //当前页号
LoadData();
}
private void LoadData()
{
int Start = 0; //当前页面开始记录行
int End = 0; //当前页面结束记录行
DataTable tableClone = table.Clone(); //克隆DataTable结构框架
#region 设置按钮的可用
if (currentPage <= 1)
{
this.toolStripButton1.Enabled = false;
this.toolStripButton2.Enabled = false;
}
else
{
toolStripButton1.Enabled = true;
this.toolStripButton2.Enabled = true;
}
if (currentPage >= pageCount)
{
toolStripButton3.Enabled = false;
toolStripButton4.Enabled = false;
}
else
{
toolStripButton3.Enabled = true;
toolStripButton4.Enabled = true;
}
#endregion
if (currentPage == pageCount)
End = sum;
else
End = row * currentPage;
Start = rowCount;
lblPageCount.Text = "/" + pageCount.ToString(); //共多少頁
txtCurrentPage.Text = Convert.ToString(currentPage);//當前頁
lab_counts.Text = "共有" + table.Rows.Count.ToString() + "条记录";
//从元数据源复制记录行
for (int i = Start; i < End; i++)
{
tableClone.ImportRow(table.Rows[i]);
rowCount++;
}
bs.DataSource = tableClone;
bindingNavigator1.BindingSource = bs;
dw_sql.DataSource = bs;
}
private void button1_Click(object sender, EventArgs e)
{
UserAdd USER = new UserAdd();
USER.Text = "添加页面";
USER.Show();
}
private void button2_Click(object sender, EventArgs e)
{
UserAdd user = new UserAdd();
user.Text = "更新页面";
user.id=Convert.ToInt32(dw_sql.CurrentRow.Cells[1].Value.ToString()); //获取当前选中行的第一个单元格的值
user.Show();
}
private void btn_delete_Click(object sender, EventArgs e)
{
try
{
int userid = Convert.ToInt32(dw_sql.CurrentRow.Cells[1].Value.ToString());
String sqlstr = "UPDATE USERS SET ENABLE=1 WHERE ID=" + userid;
int messageinfo = DB.DeleteDB(sqlstr);
if (messageinfo > 0)
{
MessageBox.Show("删除成功", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("删除失败,请联系管理员", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
String strsql = "SELECT * FROM USERS ";
DataSet ds = DB.returnDataSet(strsql);
this.dw_sql.DataSource = ds.Tables[0];
}
catch
{
MessageBox.Show("系统异常,请联系管理员", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
DB.CloseConnection();
}
}
/*
//绘制datagrridView序列号
private void dw_sql_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var dgv = sender as DataGridView;
if (dgv != null)
{
Rectangle rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rect, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
}
*/
//查询
private void toolStripButton1_Click(object sender, EventArgs e)
{
}
//
private void bindingNavigator1_ItemClicked_1(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "首页")
{
InitDataSet();
LoadData();
}
if (e.ClickedItem.Text =="末页")
{
EndDataSetPage();
}
if (e.ClickedItem.Text == "上一页")
{
currentPage--;
if (currentPage <= 0)
{
MessageBox.Show("已经是第一页,请点击“下一页”查看!");
return;
}
else
{
rowCount = row * (currentPage - 1);
}
LoadData();
}
if (e.ClickedItem.Text == "下一页")
{
currentPage++;
if (currentPage > pageCount)
{
MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
return;
}
else
{
rowCount = row * (currentPage - 1);
}
LoadData();
}
}
// 点击改变每页显示记录数
private void tsb_showPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
InitDataSet();
LoadData();
}
private void btn_query_Click(object sender, EventArgs e)
{
try
{
string username = this.txt_username.Text.Trim();
string allasname = this.txt_allasName.Text.Trim();
string value = string.Empty;
if (username.Equals("") || null == username)
{
value = "SELECT ROWNUM,ID,USERNAME,ALIASNAME,CREATEDATE,EXPIRETIME FROM USERS WHERE ALIASNAME LIKE '" + allasname + "%'AND ENABLE=0";
}
if (allasname.Equals("") || null == allasname)
{
value = "SELECT ROWNUM,ID,USERNAME,ALIASNAME,CREATEDATE,EXPIRETIME FROM USERS WHERE USERNAME LIKE '" + username + "%'AND ENABLE=0";
}
if (allasname.Equals("") && username.Equals(""))
{
value = "SELECT ROWNUM,ID,USERNAME,ALIASNAME,CREATEDATE,EXPIRETIME FROM USERS";
}
if (!username.Equals("") && !allasname.Equals(""))
{
value = "SELECT ROWNUM,ID,USERNAME,ALIASNAME,CREATEDATE,EXPIRETIME FROM USERS WHERE USERNAME LIKE '" + username + "%' AND aliasname LIKE '" + allasname + "%' AND ENABLE=0";
}
DataSet ds = DB.returnDataSet(value);
table = ds.Tables[0]; ;
InitDataSet();
LoadData();
}
catch
{
MessageBox.Show("请输入正确的SQL语句", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
}
DB.CloseConnection();
}
}
}
PS* DBHelp是一个通用的数据库操作类
本文章禁止转载,转载需说明来自本博客