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.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { int count = 10; int page; int nextpage=1; public SqlDataAdapter sql() { string sqlCon = "server=.;uid=sa;pwd=123;database=test"; SqlConnection conn = new SqlConnection(sqlCon); conn.Open(); SqlCommand comm = new SqlCommand("select * from stu", conn); SqlDataAdapter dr = new SqlDataAdapter(comm); return dr; } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dt=new DataTable(); SqlDataAdapter dr = sql(); dr.Fill(0,10,dt); if (dr.Fill(new DataSet()) % count != 0) { page = dr.Fill(new DataSet()) / count + 1; } label1.Text = nextpage.ToString(); label2.Text = page.ToString(); dataGridView1.DataSource = dt; co(); } public void co() { dataGridView1.Columns["id"].HeaderText = "学生编号"; dataGridView1.Columns["sx"].HeaderText = "数学"; dataGridView1.Columns["yy"].HeaderText = "英语"; dataGridView1.Columns["yw"].HeaderText = "语文"; for (int i = 1; i < dataGridView1.Columns.Count; i++) { for (int j = 0; j < dataGridView1.RowCount; j++) { if (int.Parse(dataGridView1[i, j].Value.ToString()) > 90) { dataGridView1[i, j].Style.BackColor = Color.GreenYellow; } else if (int.Parse(dataGridView1[i, j].Value.ToString()) < 60) { dataGridView1[i, j].Style.BackColor = Color.Red; } } } } private void button1_Click(object sender, EventArgs e) { if (nextpage < page) { DataTable dt = new DataTable(); SqlDataAdapter dr = sql(); dr.Fill(nextpage*count,count,dt); nextpage = nextpage + 1; label1.Text = nextpage.ToString(); dataGridView1.DataSource = dt; co(); } } private void button2_Click(object sender, EventArgs e) { if (nextpage > 1) { nextpage = nextpage - 1; label1.Text = nextpage.ToString(); DataTable dt = new DataTable(); SqlDataAdapter dr = sql(); dr.Fill(nextpage * count - count, count, dt); dataGridView1.DataSource = dt; co(); } } } }