我在DataGridView中有信息,我正在寻找快速过滤它的方法。
我希望它对UI中的用户很好。所以我使用TableLayoutPanel来存储和过滤文本框,我希望这个相关列与DataGridView的列大小相同。
但是在下一次运行时,dataTableProjects
列表中有6个元素
它需要大约300毫秒...我不知道为什么......
Stopwatch stopwatch = Stopwatch.StartNew();
dataGridViewProject.DataSource = dataTableProjectsFiltered;
stopwatch.Stop();
表单代码:
using ExtentionMethods;
using PulserEditor.classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PulserEditor
{
public partial class Form2 : Form
{
private BindingList<ProjectDataGridView> dataTableProjects;
private BindingList<ProjectDataGridView> dataTableProjectsFiltered;
public Form2()
{
InitializeComponent();
UpdateProjectsGrid();
tableLayoutPanelCatalogs.ColumnStyles[0].Width = dataGridViewProject.RowHeadersWidth;
dataGridViewProject.ColumnWidthChanged += dataGridViewProject_ColumnWidthChanged;
}
private void UpdateProjectsGrid()
{
dataTableProjects = PulserEditorDataBaseManager.GetProjects();
dataTableProjectsFiltered = new BindingList<ProjectDataGridView>(dataTableProjects.ToList());
dataGridViewProject.DataSource = dataTableProjectsFiltered;
}
private void textBoxFilterCatalogByDescription_TextChanged(object sender, EventArgs e)
{
FilterProjects();
}
private void textBoxFilterCatalogByCatalogNumber_TextChanged(object sender, EventArgs e)
{
FilterProjects();
}
private void textBoxFilterCatalogByCellCode_TextChanged(object sender, EventArgs e)
{
FilterProjects();
}
private void FilterProjects()
{
if (dataTableProjects != null)
{
dataTableProjectsFiltered = new BindingList<ProjectDataGridView>(dataTableProjects.Where(
p => p.Teur_Project.ContainsConsts(textBoxFilterCatalogByDescription.Text) &&
p.Code_Project.ContainsConsts(textBoxFilterCatalogByCatalogNumber.Text) &&
p.Code_age_cell.ContainsConsts(textBoxFilterCatalogByCellCode.Text)).ToList());
dataGridViewProject.ColumnWidthChanged -= dataGridViewProject_ColumnWidthChanged;
Stopwatch stopwatch = Stopwatch.StartNew();
dataGridViewProject.DataSource = dataTableProjectsFiltered;
stopwatch.Stop();
dataGridViewProject.ColumnWidthChanged += dataGridViewProject_ColumnWidthChanged;
}
}
private void dataGridViewProject_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
tableLayoutPanelCatalogs.ColumnStyles[e.Column.Index + 1].Width = e.Column.Width;
}
}
}
扩展方法
public static bool CompareConsts(this string str, string other)
{
return str.Trim().ToLower().Equals(other.Trim().ToLower());
}
public static bool ContainsConsts(this string str, string other)
{
return str.Trim().ToLower().Contains(other.Trim().ToLower());
}
来自数据库的电话......
public static BindingList<ProjectDataGridView> GetProjects() {
BindingList<ProjectDataGridView> projectList = new BindingList<ProjectDataGridView>();
try
{
using (SqlConnection openCon = new SqlConnection(connectionString))
{
string saveStaff = "select [Teur_Project],[Project].[Code_Project],[Code_age_cell]," +
"[Current],[time],[Cells_in_Parallel],[Cells_in_Series],[dADocv_min],[dAdccv_min]," +
"[dADocv_max],[dAdccv_max],[Current_qc],[Time_QC],[dADocv_QC],[dAdccv_QC],[Department] " +
"from(Select[Code_Project], MAX([Change]) as maxDate " +
"from [Project] " +
"group by [Code_Project]) as MaxTbl, [Project] " +
"where MaxTbl.[maxDate] = [Project].[Change] and [Active]=@active";
SqlCommand command = new SqlCommand(saveStaff, openCon);
command.Parameters.AddWithValue("active", true);
openCon.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
projectList.Add(new ProjectDataGridView(dr.GetString(0).Trim(), dr.GetString(1).Trim(),
dr.GetString(2).Trim(),dr.GetInt64(3), dr.GetInt64(4), dr.GetInt32(5), dr.GetInt32(6),
dr.GetDouble(7), dr.GetDouble(8),dr.GetDouble(9), dr.GetDouble(10), dr.GetInt64(11),
dr.GetInt64(12), dr.GetDouble(13),dr.GetDouble(14),dr.GetInt32(15)));
}
openCon.Close();
return projectList;
}
}
catch (Exception ex)
{
WriteLog(ex.StackTrace, ex.Message);
}
return null;
}