using System; using WeifenLuo.WinFormsUI.Docking; namespace 成套报价软件 { /// 停靠窗口:选型。内容在本类的设计器视图中编辑。 public partial class DockWindow3 : DockContent { public DockWindow3() { InitializeComponent(); this.BackColor = System.Drawing.Color.White; Text = "选型"; TabText = "选型"; // ★ 筛选(2026-10-02):comboBox1 输入关键字,button1 对汇总表(advancedDataGridView1) // 的 元件名称/规格/图纸规格 三列做模糊筛选;关键字为空时点击=清除筛选 comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown; // 可手动输入 button1.Click += button1_筛选_Click; comboBox1.KeyDown += (s, e) => { if (e.KeyCode == System.Windows.Forms.Keys.Enter) { button1_筛选_Click(s, e); e.SuppressKeyPress = true; // 防回车 beep } }; } /// 筛选/清除:对汇总表(advancedDataGridView1)的 元件名称/规格/图纸规格 /// 三列任一包含关键字即显示(RowFilter 过滤视图,不动数据源,刷新加载自动重置)。 /// 按钮状态机:Text="清除"(筛选生效中)→点击=无条件清除;Text="筛选"→按关键字筛选,空关键字=清除。 private void button1_筛选_Click(object sender, EventArgs e) { try { var dt = advancedDataGridView1.DataSource as System.Data.DataTable; if (dt == null) return; // 尚未加载数据 // ★ 筛选生效中(按钮显示"清除"):点击即无条件清除(关键字保留在框里便于改词再筛) if (button1.Text == "清除") { dt.DefaultView.RowFilter = ""; button1.Text = "筛选"; return; } string kw = (comboBox1.Text ?? "").Trim(); if (kw.Length == 0) { dt.DefaultView.RowFilter = ""; // 空关键字 = 清除(双保险路径) button1.Text = "筛选"; return; } // RowFilter LIKE 通配符/引号转义 string esc = kw.Replace("'", "''").Replace("[", "[[]").Replace("%", "[%]").Replace("*", "[*]"); string like = " LIKE '%" + esc + "%'"; dt.DefaultView.RowFilter = "元件名称" + like + " OR 规格" + like + " OR 图纸规格" + like; button1.Text = "清除"; // 关键字入下拉历史(去重置顶,最多 20 条) int idx = comboBox1.Items.IndexOf(kw); if (idx >= 0) comboBox1.Items.RemoveAt(idx); comboBox1.Items.Insert(0, kw); while (comboBox1.Items.Count > 20) comboBox1.Items.RemoveAt(comboBox1.Items.Count - 1); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(this, "筛选失败:" + ex.Message, "选型", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } } } }