C#编程-74:dataGridView排序和筛选_彭世瑜_新浪博客

161 阅读1分钟
  1. using System;

  2. using System.ComponentModel;

  3. using System.Data;

  4. using System.Windows.Forms;

  5.  

  6. namespace DataGridViewSort

  7. {

  8.     public partial class Form1 : Form

  9.     {

  10.         public Form1()

  11.         {

  12.             InitializeComponent();

  13.         }

  14.  

  15.         private void Form1_Load(object sender, EventArgs e)

  16.         {

  17.             // TODO:  这行代码将数据加载到表“companyDataSet.clerk”中。您可以根据需要移动或删除它。

  18.             this.clerkTableAdapter.Fill(this.companyDataSet.clerk);

  19.  

  20.         }

  21.  

  22.         //通过表格列排序实现

  23.         private void button1_Click(object sender, EventArgs e)

  24.         {

  25.             this.dataGridView1.Sort(dataGridView1.Columns[3],ListSortDirection.Ascending);

  26.         }

  27.  

  28.         //通过更改数据源实现筛选

  29.         private void button2_Click(object sender, EventArgs e)

  30.         {

  31.             DataView dv = new DataView(this.companyDataSet.clerk, "department='研发部'", "age Asc", DataViewRowState.CurrentRows);

  32.             dataGridView1.DataSource = dv;

  33.  

  34.         }

  35.     }

  36. }

\

备注:在dataview对象中筛选不需要加“N”

\