当前位置:网站首页>[C# language] DataGridView sorting and filtering
[C# language] DataGridView sorting and filtering
2022-08-07 19:23:00 【Wimbs】
In practical applications, the data in the data table needs to be sorted and filtered.
Method:
Sorts the contents of the System.Windows.Forms.DataGridView control in ascending or descending order based on the contents of the specified column.
public virtual void Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction);
Initializes a new instance of the System.Data.DataView class with the specified System.Data.DataTable, System.Data.DataView.RowFilter, System.Data.DataView.Sort, and System.Data.DataViewRowState.
public DataView(DataTable table, string RowFilter, string Sort, DataViewRowState RowState);



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;namespace Test_DataGridView{public partial class Form1 : Form{public Form1(){InitializeComponent();}// global variableint rowIndex;private void Form1_Load(object sender, EventArgs e){dataGridView1.DataSource = Source();}//Data table resourceprivate DataTable Source(){DataTable mydt = new DataTable();mydt.Columns.Add("Name");mydt.Columns.Add("Age");mydt.Columns.Add("Score");String[,] str = new String[,] { { "Zhang San", "21", "90" }, { "Li Si", "22", "93" }, { "Wang Wu", "23", "99" } };for (int i = 0; i < 3; i++){DataRow dr = mydt.NewRow();dr[0] = str[i, 0];dr[1] = str[i, 1];dr[2] = str[i, 2];mydt.Rows.Add(dr);}return mydt;}//get cell informationprivate void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){//get cell coordinatesint col = dataGridView1.CurrentCellAddress.X + 1;int row = dataGridView1.CurrentCellAddress.Y + 1;//get cell contentString content = dataGridView1.CurrentCell.Value.ToString();MessageBox.Show("row: " + row.ToString() + " column: " + col.ToString() + " content: " + content);}// hide ageprivate void button1_Click(object sender, EventArgs e){String bts = button1.Text;if (bts == "hidden age"){dataGridView1.Columns[1].Visible = false;bts = "Show age";button1.Text = bts;}else{dataGridView1.Columns[1].Visible = true;bts = "Hide Age";button1.Text = bts;}}//Right click eventprivate void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e){if (e.Button == MouseButtons.Right){this.dataGridView1.Rows[e.RowIndex].Selected = true;this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];this.contextMenuStrip1.Show(this.dataGridView1, e.Location);contextMenuStrip1.Show(Cursor.Position);rowIndex = e.RowIndex;}}// delete the content of the row eventprivate void delete line ToolStripMenuItem_Click(object sender, EventArgs e){if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow){this.dataGridView1.Rows.RemoveAt(rowIndex);}}//sort buttonprivate void button2_Click(object sender, EventArgs e){dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Descending);}// filter buttonprivate void button3_Click(object sender, EventArgs e){DataView dv = new DataView(Source(), "Name='Li Si'", "Age", DataViewRowState.CurrentRows);dataGridView1.DataSource = dv;}}}边栏推荐
- [Educational Codeforces Round 133 F] Bags with Balls (组合计数 推式子)
- 数据集中存在错误标注怎么办? 置信学习帮你解决
- Database Notes
- Suggestions for amh database import function?
- [C# language] DataGridView hides rows and columns
- makefile小技巧
- DELL SC康贝存储忘记密码的重置方法
- Chaoyang District and 360 Group jointly build a security service platform for SMEs
- #C语言大小写转换#
- 翻译软件在线翻译
猜你喜欢

OpenHarmony像素单位

Language Translator - Online Language Translator Free
![[chestnut sugar GIS] DOS - how to delete a specific folder](/img/d9/917dcfa2508695f01f4e2fb33100df.png)
[chestnut sugar GIS] DOS - how to delete a specific folder

数学符号 参考手册大全

云原生核心技术(一文搞懂云原生)

职场经验分享--接口中按时间戳查数据容易被忽略的细节

English Convert - Online English Batch Converter Free

Free translation software - batch automatic one-click translation
![[chestnut sugar GIS] DOS - how to extract the folder name](/img/c9/3e37571faf98fad6012152c9ffc5bd.png)
[chestnut sugar GIS] DOS - how to extract the folder name

百万年薪架构师谈:掌握这【6+2】学习路线 进BAT拿月薪40k真不难
随机推荐
English Convert - Online English Batch Converter Free
mongoDB数据库
Transformer pytorch实现逐行详解
Language Translator - Online Language Translator Free
CSDN竞赛第三期 快递员、小鱼游泳题解
win10上使用VS2017编译libcurl
语法基础(变量、输入输出、表达式与顺序语句)
国内多位架构大牛强烈推荐的大型分布式手册
R语言使用Rtsne包进行TSNE分析:提取TSNE分析结果合并到原dataframe中、可视化tsne降维的结果、并使用两个分类变量从颜色、形状两个角度来可视化tsne降维的效果
Exchange Comprehensive Experiment
Three ways to splicing int type variables and String type variables in dart
朝阳区联合360集团共同打造中小企业安全服务平台
Json格式数据以及QT操作Json数据
UEditorPlus v2.3.0发布 图片抓取重构,多处样式优化
单独使用return关键字
cesium 源码分析 裁切面原理
playbook中template、tags、roles操作示例
XSS 攻击是什么?
数据集中存在错误标注怎么办? 置信学习帮你解决
基于FTP协议的文件上传与下载