winform 的 datagridview 动态刷新
要实现在 datagridview 中动态的增删改查,目前能用的比较友好的方案似乎只有 datatable,网上有人说用 BindingList 的,我替你们试过了,无法筛选,坑很多
public partial class Form1 : Form { private DataTable dataTable = new DataTable(); private BindingSource bindingSource; public Form1() { InitializeComponent(); dataGridView1.AllowUserToAddRows = false; } private void Form1_Load(object sender, EventArgs e) { //创建自增列 dataTable.Columns.Add(new DataColumn("Id", typeof(int)) { Caption = "ID", AutoIncrement = true, AutoIncrementSeed = 1 }); //创建字符串列 dataTable.Columns.Add(new DataColumn("Name", typeof(string)) { Caption = "姓名" }); //创建一个数值列 dataTable.Columns.Add(new DataColumn("Age", typeof(int)) { Caption = "年龄" }); //创建一个object列 dataTable.Columns.Add(new DataColumn("Data", typeof(object)) { Caption = "数据" }); //添加一个计算列,由其它列数据生成 dataTable.Columns.Add(new DataColumn("BirthYear", typeof(int), DateTime.Now.Year.ToString() + " - Age") { Caption = "出生年份" }); //创建数据 for (var i = 0; i < 10; i++) { var row = dataTable.NewRow(); row["Name"] = "Name" + (i + 1).ToString(); row["Age"] = i + 20; dataTable.Rows.Add(row); } bindingSource = new BindingSource(); bindingSource.DataSource = new DataView(dataTable); dataGridView1.DataSource = bindingSource; //更新表头 foreach (DataColumn column in dataTable.Columns) { string columnName = column.ColumnName; string columnCaption = column.Caption; //隐藏显示不同的列 if (columnName == "Data") column.ExtendedProperties["Visible"] = false; else column.ExtendedProperties["Visible"] = true; dataGridView1.Columns[columnName].Visible = Convert.ToBoolean(column.ExtendedProperties["Visible"]); // 使用 Caption 属性作为列标题,如果 Caption 为空,则使用 ColumnName dataGridView1.Columns[columnName].HeaderText = string.IsNullOrEmpty(columnCaption) ? columnName : columnCaption; } } private void btnFilter_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(bindingSource.Filter)) bindingSource.Filter = "Age > 25"; else bindingSource.Filter = string.Empty; } private void btnAdd_Click(object sender, EventArgs e) { var i = dataTable.Rows.Count ; var row = dataTable.NewRow(); row["Name"] = "Name" + (i + 1).ToString(); row["Age"] = i + 20; dataTable.Rows.Add(row); } private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { var column = dataGridView1.Columns[e.ColumnIndex]; if (column.HeaderCell.Tag == null) { column.HeaderCell.Tag = 1; column.HeaderCell.SortGlyphDirection = SortOrder.Ascending; } else { column.HeaderCell.Tag = null; column.HeaderCell.SortGlyphDirection = SortOrder.Descending; } // 执行排序 var direction = column.HeaderCell.SortGlyphDirection == SortOrder.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending; dataGridView1.Sort(column, direction); } private void btnDelete_Click(object sender, EventArgs e) { if (dataTable.Rows.Count > 0) { dataTable.Rows[0].Delete(); dataTable.AcceptChanges(); } } private void btnQuery_Click(object sender, EventArgs e) { DataRow[] rows = dataTable.Select("Age = 25"); foreach (var row in rows) { Console.WriteLine("Name:" + row["Name"].ToString()); } } }
桂棹兮兰桨,击空明兮溯流光。