C#编程-81:DataGridView常用操作综合实例

C#编程-81:DataGridView常用操作综合实例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace DataGridViewLastTest
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.          
  19.         DataTable dt;
  20.         int valTotal = 500;//总数量       
  21.         int valPerPage = 9;//每页条数
  22.  
  23.         int pageNum = 0;//总页数
  24.         int currentPage = 1;//当前页
  25.         int valCurrent = 0;//当前条数
  26.         int valStartIndex = 0;//每页起始条数
  27.         int valEndIndex = 0;//当前页终止条数
  28.  
  29.         //生成数据表
  30.         private void GenerageData()
  31.         {
  32.             dt = new DataTable("ClerkSalary");
  33.             dt.Columns.Add("姓名", Type.GetType("System.String"));
  34.             dt.Columns.Add("姓别", Type.GetType("System.String"));
  35.             dt.Columns.Add("工资", Type.GetType("System.Int32"));
  36.             string familyName = "赵钱孙李周吴郑冯陈褚卫蒋沈韩杨朱秦尤许何吕施张";
  37.             string lastName=@"那日丹桂梢头缀黄花绽竹篱抛情引墨顿成诗情也依依爱也依依
  38.                             天涯两相望月人独伤悲红笺小字寄相思情也难追爱也难追
  39.                             聊期花共月闲看绿着珠滴水涌泉歌诗儒词也狂书笔也狂书
  40.                             瑶琴闲置久知音日渐疏往事思来总不如词也成枯笔也成枯";
  41.             string gender = "男女";
  42.             Random random = new Random();
  43.             for (int i = 0; i < valTotal; i++)
  44.             {
  45.                 //新增行方法一:
  46.                 //DataRow dr = dt.NewRow();
  47.                 //dr[0] = familyName[random.Next(0, familyName.Length)].ToString()
  48.                 //    + lastName[random.Next(0, lastName.Length)].ToString()
  49.                 //    + lastName[random.Next(0, lastName.Length)].ToString();
  50.                 //dr[1] = gender[random.Next(0, gender.Length)].ToString();
  51.                 //dr[2] = random.Next(1800, 10000);
  52.                 //dt.Rows.Add(dr);
  53.  
  54.                 //新增行方法二:
  55.                 string name = familyName[random.Next(0, familyName.Length)].ToString()
  56.                     +lastName[random.Next(0, lastName.Length)].ToString() 
  57.                     +lastName[random.Next(0, lastName.Length)].ToString();
  58.                 string Gender=gender[random.Next(0,gender.Length)].ToString();
  59.                 int salary = random.Next(1800,10000);
  60.                 dt.Rows.Add(new object[] { name,Gender,salary});
  61.             }
  62.          
  63.         }
  64.  
  65.         //加载当前页的数据
  66.         private void LoadData()
  67.         {
  68.             DataTable dtTemp = dt.Clone();
  69.             //只有一页
  70.             if (currentPage == pageNum) valEndIndex = valTotal - 1;
  71.             //不止一页
  72.             else valEndIndex = currentPage * valPerPage;
  73.             valStartIndex = valCurrent;
  74.             //更新显示
  75.             txtCurrentPage.Text = currentPage.ToString();
  76.             lblTotalPage.Text = "/" + Convert.ToString(pageNum);
  77.             //从数据表中读取当前数据
  78.             for (int i = valStartIndex; i <= valEndIndex; i++)
  79.             {
  80.                 dtTemp.ImportRow(dt.Rows[i]);
  81.                 valCurrent++;
  82.             }
  83.             //通过bindingSource1作为桥梁绑定数据
  84.             bindingSource1.DataSource = dtTemp;
  85.             bindingNavigator1.BindingSource = bindingSource1;
  86.             dataGridView1.DataSource = bindingSource1;
  87.             //设置不同行显示背景
  88.             dataGridView1.RowsDefaultCellStyle.BackColor = Color.Pink;
  89.             dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Violet;
  90.         }
  91.    private void Form1_Load(object sender, EventArgs e)
  92.         {
  93.             try
  94.             {
  95.                 GenerageData();
  96.                 pageNum = (valTotal % valPerPage == 0) ? (valTotal / valPerPage) : (valTotal / valPerPage + 1);
  97.                 LoadData();
  98.             }
  99.             catch (Exception ex)
  100.             {
  101.                  
  102.                 throw;
  103.             }
  104.         }
  105.          
  106.         private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  107.         {
  108.             //绘制行号
  109.             Rectangle rect = new Rectangle(e.RowBounds.Location.X,e.RowBounds.Location.Y,dataGridView1.RowHeadersWidth,e.RowBounds.Height);
  110.             TextRenderer.DrawText(e.Graphics,(e.RowIndex+1+valStartIndex).ToString(),dataGridView1.RowHeadersDefaultCellStyle.Font,rect,dataGridView1.RowHeadersDefaultCellStyle.ForeColor,TextFormatFlags.VerticalCenter|TextFormatFlags.Right);
  111.             //选中时显示不同的背景
  112.             if (e.RowIndex >= dataGridView1.Rows.Count - 1) return;
  113.             var row =dataGridView1.Rows[e.RowIndex];
  114.             Color oldForeColor = new Color();
  115.             Color oldBackColor = new Color();
  116.             if (row == dataGridView1.CurrentRow)
  117.             {
  118.                 if (row.DefaultCellStyle.ForeColor != Color.White)
  119.                 {
  120.                     oldForeColor = row.DefaultCellStyle.ForeColor;
  121.                     row.DefaultCellStyle.ForeColor = Color.White;
  122.                 }
  123.                 if (row.DefaultCellStyle.BackColor != Color.Blue)
  124.                 {
  125.                     oldBackColor = row.DefaultCellStyle.BackColor;
  126.                     row.DefaultCellStyle.BackColor = Color.Blue;
  127.                 }
  128.  
  129.             }
  130.             else
  131.             {
  132.                 row.DefaultCellStyle.ForeColor = oldForeColor;
  133.                 row.DefaultCellStyle.BackColor = oldBackColor;
  134.             }
  135.              
  136.         }
  137.  
  138.         private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  139.         {
  140.             if (e.ClickedItem.Text == "上一页")
  141.             {
  142.                 currentPage--;
  143.                 if (currentPage <= 0)
  144.                 {
  145.                     MessageBox.Show("已经是第一页");
  146.                     currentPage++;
  147.                     return;
  148.                 }
  149.                 else
  150.                 
  151.                     valCurrent=valPerPage*(currentPage-1);
  152.                 }
  153.                 LoadData();
  154.             }
  155.             if (e.ClickedItem.Text =="下一页")
  156.             {
  157.                 currentPage++;
  158.                 if (currentPage > pageNum)
  159.                 {
  160.                     currentPage--;
  161.                     MessageBox.Show("已经是最后一页");
  162.                     return;
  163.                 }
  164.                 else
  165.                 {
  166.                     valCurrent = valPerPage * (currentPage - 1);
  167.                 }
  168.                 LoadData();
  169.             }
  170.              
  171.         }
  172.     }
  173. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值