* UIComboDataGridView:增加过滤

This commit is contained in:
Sunny 2021-11-05 22:01:53 +08:00
parent 70f47568db
commit f7c70b12a9
7 changed files with 201 additions and 39 deletions

Binary file not shown.

Binary file not shown.

View File

@ -491,15 +491,19 @@ namespace Sunny.UI.Demo
//
this.uiComboDataGridView1.DropDownStyle = Sunny.UI.UIDropDownStyle.DropDownList;
this.uiComboDataGridView1.FillColor = System.Drawing.Color.White;
this.uiComboDataGridView1.FilterColomnName = null;
this.uiComboDataGridView1.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiComboDataGridView1.Location = new System.Drawing.Point(386, 292);
this.uiComboDataGridView1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.uiComboDataGridView1.MinimumSize = new System.Drawing.Size(63, 0);
this.uiComboDataGridView1.Name = "uiComboDataGridView1";
this.uiComboDataGridView1.Padding = new System.Windows.Forms.Padding(0, 0, 30, 2);
this.uiComboDataGridView1.ShowFilter = false;
this.uiComboDataGridView1.Size = new System.Drawing.Size(308, 29);
this.uiComboDataGridView1.TabIndex = 74;
this.uiComboDataGridView1.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
this.uiComboDataGridView1.SelectIndexChange += new Sunny.UI.UIDataGridView.OnSelectIndexChange(this.uiComboDataGridView1_SelectIndexChange_1);
this.uiComboDataGridView1.ValueChanged += new Sunny.UI.UIComboDataGridView.OnValueChanged(this.uiComboDataGridView1_ValueChanged);
//
// uiButton1
//

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
namespace Sunny.UI.Demo
@ -30,40 +31,29 @@ namespace Sunny.UI.Demo
uiComboDataGridView1.DataGridView.ReadOnly = true;
uiComboDataGridView1.SelectIndexChange += UiComboDataGridView1_SelectIndexChange;
for (int i = 0; i < 20; i++)
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
for (int i = 0; i < 100; i++)
{
Data data = new Data();
data.Column1 = "Data" + i.ToString("D2");
data.Column2 = i.Mod(2) == 0 ? "A" : "B";
data.Column3 = "编辑";
datas.Add(data);
dt.Rows.Add("A" + i.ToString("D2"),
"B" + (i + 1).ToString("D2"),
"C" + (i + 2).ToString("D2"));
}
uiComboDataGridView1.DataGridView.DataSource = datas;
uiComboDataGridView1.ShowFilter = true;
uiComboDataGridView1.DataGridView.DataSource = dt;
uiComboDataGridView1.FilterColomnName = "Column1"; //不设置则全部列过滤
}
private void UiComboDataGridView1_SelectIndexChange(object sender, int index)
{
uiComboDataGridView1.Text = datas[index].ToString();
uiComboDataGridView1.Text = dt.Rows[index]["Column1"].ToString();
}
List<Data> datas = new List<Data>();
public class Data
{
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public bool Column4 { get; set; }
public override string ToString()
{
return Column1;
}
}
DataTable dt = new DataTable();
public class Info
{
@ -124,5 +114,22 @@ namespace Sunny.UI.Demo
{
uiComboBox1.ShowDropDown();
}
private void uiComboDataGridView1_ValueChanged(object sender, object value)
{
if (value is DataGridViewRow row)
{
uiComboDataGridView1.Text = row.Cells["Column1"].Value.ToString();
}
else
{
uiComboDataGridView1.Text = "";
}
}
private void uiComboDataGridView1_SelectIndexChange_1(object sender, int index)
{
uiComboDataGridView1.Text = dt.Rows[index]["Column1"].ToString();
}
}
}

View File

@ -1,10 +1,18 @@
namespace Sunny.UI
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
namespace Sunny.UI
{
public class UIComboDataGridViewItem : UIDropDownItem, ITranslate
{
private UIPanel panel;
private UISymbolButton btnCancel;
private UISymbolButton btnOK;
private UIPanel pFilter;
private UISymbolButton btnSearch;
private UITextBox edtFilter;
private UISymbolButton btnClear;
private UIDataGridView dataGridView;
public UIComboDataGridViewItem()
@ -25,6 +33,8 @@
set => panel.Visible = value;
}
public string FilterColomnName { get; set; }
public UIDataGridView DataGridView => dataGridView;
private void InitializeComponent()
@ -38,8 +48,13 @@
this.btnCancel = new Sunny.UI.UISymbolButton();
this.btnOK = new Sunny.UI.UISymbolButton();
this.dataGridView = new Sunny.UI.UIDataGridView();
this.pFilter = new Sunny.UI.UIPanel();
this.btnSearch = new Sunny.UI.UISymbolButton();
this.edtFilter = new Sunny.UI.UITextBox();
this.btnClear = new Sunny.UI.UISymbolButton();
this.panel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.pFilter.SuspendLayout();
this.SuspendLayout();
//
// panel
@ -48,14 +63,14 @@
this.panel.Controls.Add(this.btnOK);
this.panel.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.panel.Location = new System.Drawing.Point(0, 194);
this.panel.Location = new System.Drawing.Point(0, 289);
this.panel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.panel.MinimumSize = new System.Drawing.Size(1, 1);
this.panel.Name = "panel";
this.panel.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
this.panel.RectSides = ((System.Windows.Forms.ToolStripStatusLabelBorderSides)(((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left | System.Windows.Forms.ToolStripStatusLabelBorderSides.Right)
| System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom)));
this.panel.Size = new System.Drawing.Size(385, 44);
this.panel.Size = new System.Drawing.Size(569, 44);
this.panel.TabIndex = 2;
this.panel.Text = null;
this.panel.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
@ -70,7 +85,7 @@
this.btnCancel.FillPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(202)))), ((int)(((byte)(87)))), ((int)(((byte)(89)))));
this.btnCancel.FillSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(202)))), ((int)(((byte)(87)))), ((int)(((byte)(89)))));
this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.btnCancel.Location = new System.Drawing.Point(294, 8);
this.btnCancel.Location = new System.Drawing.Point(478, 8);
this.btnCancel.MinimumSize = new System.Drawing.Size(1, 1);
this.btnCancel.Name = "btnCancel";
this.btnCancel.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(80)))), ((int)(((byte)(80)))));
@ -90,7 +105,7 @@
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnOK.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.btnOK.Location = new System.Drawing.Point(205, 8);
this.btnOK.Location = new System.Drawing.Point(389, 8);
this.btnOK.MinimumSize = new System.Drawing.Size(1, 1);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(80, 29);
@ -98,7 +113,7 @@
this.btnOK.Text = "确定";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// uiDataGridView
// dataGridView
//
dataGridViewCellStyle6.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
this.dataGridView.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle6;
@ -125,8 +140,8 @@
this.dataGridView.EnableHeadersVisualStyles = false;
this.dataGridView.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.dataGridView.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(160)))), ((int)(((byte)(255)))));
this.dataGridView.Location = new System.Drawing.Point(0, 0);
this.dataGridView.Name = "uiDataGridView";
this.dataGridView.Location = new System.Drawing.Point(0, 44);
this.dataGridView.Name = "dataGridView";
dataGridViewCellStyle9.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle9.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
dataGridViewCellStyle9.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
@ -141,17 +156,84 @@
this.dataGridView.RowTemplate.Height = 25;
this.dataGridView.SelectedIndex = -1;
this.dataGridView.ShowGridLine = true;
this.dataGridView.Size = new System.Drawing.Size(385, 194);
this.dataGridView.Size = new System.Drawing.Size(569, 245);
this.dataGridView.TabIndex = 3;
//
// pFilter
//
this.pFilter.Controls.Add(this.btnClear);
this.pFilter.Controls.Add(this.btnSearch);
this.pFilter.Controls.Add(this.edtFilter);
this.pFilter.Dock = System.Windows.Forms.DockStyle.Top;
this.pFilter.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.pFilter.Location = new System.Drawing.Point(0, 0);
this.pFilter.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.pFilter.MinimumSize = new System.Drawing.Size(1, 1);
this.pFilter.Name = "pFilter";
this.pFilter.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
this.pFilter.RectSides = ((System.Windows.Forms.ToolStripStatusLabelBorderSides)(((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left | System.Windows.Forms.ToolStripStatusLabelBorderSides.Top)
| System.Windows.Forms.ToolStripStatusLabelBorderSides.Right)));
this.pFilter.Size = new System.Drawing.Size(569, 44);
this.pFilter.TabIndex = 4;
this.pFilter.Text = null;
this.pFilter.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnSearch
//
this.btnSearch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnSearch.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnSearch.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.btnSearch.Location = new System.Drawing.Point(389, 8);
this.btnSearch.MinimumSize = new System.Drawing.Size(1, 1);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(80, 29);
this.btnSearch.Symbol = 61442;
this.btnSearch.TabIndex = 1;
this.btnSearch.Text = "搜索";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// edtFilter
//
this.edtFilter.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.edtFilter.ButtonSymbol = 61761;
this.edtFilter.Cursor = System.Windows.Forms.Cursors.IBeam;
this.edtFilter.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.edtFilter.Location = new System.Drawing.Point(17, 10);
this.edtFilter.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.edtFilter.Maximum = 2147483647D;
this.edtFilter.Minimum = -2147483648D;
this.edtFilter.MinimumSize = new System.Drawing.Size(1, 16);
this.edtFilter.Name = "edtFilter";
this.edtFilter.Size = new System.Drawing.Size(363, 25);
this.edtFilter.TabIndex = 0;
this.edtFilter.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
//
// btnClear
//
this.btnClear.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnClear.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnClear.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.btnClear.Location = new System.Drawing.Point(478, 8);
this.btnClear.MinimumSize = new System.Drawing.Size(1, 1);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(80, 29);
this.btnClear.Symbol = 61666;
this.btnClear.SymbolSize = 22;
this.btnClear.TabIndex = 2;
this.btnClear.Text = "清除";
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// UIComboDataGridViewItem
//
this.Controls.Add(this.dataGridView);
this.Controls.Add(this.pFilter);
this.Controls.Add(this.panel);
this.Name = "UIComboDataGridViewItem";
this.Size = new System.Drawing.Size(385, 238);
this.Size = new System.Drawing.Size(569, 333);
this.panel.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.pFilter.ResumeLayout(false);
this.ResumeLayout(false);
}
@ -159,7 +241,13 @@
private void btnOK_Click(object sender, System.EventArgs e)
{
if (dataGridView.RowCount > 0 && dataGridView.SelectedIndex >= 0)
{
if (ShowFilter)
DoValueChanged(this, dataGridView.SelectedRows[0]);
else
DoValueChanged(this, dataGridView.SelectedIndex);
}
CloseParent();
}
@ -167,5 +255,53 @@
{
CloseParent();
}
public bool ShowFilter
{
get => pFilter.Visible;
set =>pFilter.Visible = value;
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
string filter = "";
if (edtFilter.Text.IsNullOrEmpty())
{
filter = "";
}
else
{
if (FilterColomnName.IsValid())
{
string str = FilterColomnName + " like '%" + edtFilter.Text + "%'";
filter = str;
}
else
{
List<string> strings = new List<string>();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
if (column.Visible)
{
strings.Add(column.HeaderText + " like '%" + edtFilter.Text + "%'");
}
}
filter = string.Join(" or ", strings);
}
}
if (dataGridView.DataSource is DataTable table)
{
table.DefaultView.RowFilter = filter;
}
}
private void btnClear_Click(object sender, System.EventArgs e)
{
edtFilter.Text = "";
btnSearch.PerformClick();
DoValueChanged(this, null);
}
}
}

View File

@ -17,6 +17,7 @@
* : 2021-09-01
*
* 2020-09-01: V3.0.6
* 2021-11-05: V3.0.8
******************************************************************************/
using System;
@ -27,6 +28,7 @@ using static Sunny.UI.UIDataGridView;
namespace Sunny.UI
{
[DefaultProperty("ValueChanged")]
[ToolboxItem(true)]
public class UIComboDataGridView : UIDropControl, IToolTip
{
@ -47,6 +49,8 @@ namespace Sunny.UI
private void UIComboDataGridView_ButtonClick(object sender, EventArgs e)
{
item.FilterColomnName = FilterColomnName;
item.ShowFilter = ShowFilter;
ItemForm.Size = ItemSize;
item.ShowButtons = true;
item.Translate();
@ -73,6 +77,8 @@ namespace Sunny.UI
if (item != null) item.DataGridView.Font = Font;
}
public bool ShowFilter { get; set; }
private readonly UIComboDataGridViewItem item = new UIComboDataGridViewItem();
protected override void CreateInstance()
@ -84,9 +90,18 @@ namespace Sunny.UI
public event OnSelectIndexChange SelectIndexChange;
public delegate void OnValueChanged(object sender, object value);
public event OnValueChanged ValueChanged;
protected override void ItemForm_ValueChanged(object sender, object value)
{
SelectIndexChange?.Invoke(this, value.ToString().ToInt());
if (ShowFilter)
ValueChanged?.Invoke(this, value);
else
SelectIndexChange(this, value.ToString().ToInt());
}
public string FilterColomnName { get; set; }
}
}

View File

@ -35,7 +35,7 @@ namespace Sunny.UI
[DefaultProperty("Nodes")]
public sealed partial class UINavBar : ScrollableControl, IStyleInterface
{
public readonly UINavMenu Menu = new UINavMenu();
public readonly TreeView Menu = new TreeView();
private readonly UIContextMenuStrip NavBarMenu = new UIContextMenuStrip();