编写winform 自定义控件,控件具备输入搜索条件的功能同时具备显示搜索结果的功能,显示用的是DataGridView控件,搜索用的TextBox位于DataGridView控件的第一行中,收缩条件与DatagridView的列绑定

时间: 2024-02-01 09:14:51 浏览: 29
首先,创建一个新的WinForm控件库项目,命名为MySearchControl。 然后,在Visual Studio的工具箱中,右键单击并选择“添加项目”>“新建项”,创建一个DataGridView控件,命名为MyDataGridView。 在MyDataGridView中,添加一个TextBox控件作为搜索条件的输入框,将其放置在DataGridView的第一行中。同时,将其绑定到DataGridView的一个列上,以便在搜索时进行筛选。 接下来,在MySearchControl中创建一个UserControl控件,命名为MySearchControl,该控件包含MyDataGridView和一个搜索按钮。 在MySearchControl中,我们需要在构造函数中初始化DataGridView控件,并将其添加到UserControl中。同时,我们还需要为搜索按钮添加一个Click事件处理程序,该处理程序将使用搜索条件从DataGridView中筛选结果。 最后,将MySearchControl控件添加到WinForm中并测试它是否正常工作。 以下是代码示例: MyDataGridView.cs: ```csharp using System.Windows.Forms; namespace MySearchControl { public partial class MyDataGridView : DataGridView { public MyDataGridView() { InitializeComponent(); this.AllowUserToAddRows = false; this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; this.RowHeadersVisible = false; this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; this.MultiSelect = false; this.AllowUserToResizeRows = false; this.AllowUserToResizeColumns = false; this.EditMode = DataGridViewEditMode.EditProgrammatically; this.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; // Add a TextBox control to the first row DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = "Search"; col.SortMode = DataGridViewColumnSortMode.NotSortable; this.Columns.Add(col); this.Rows.Add(); this.Controls.Add(new TextBox()); this.Controls[0].Top = this.GetCellDisplayRectangle(0, 0, true).Top + 2; this.Controls[0].Left = this.GetCellDisplayRectangle(0, 0, true).Left + 2; this.Controls[0].Width = this.GetCellDisplayRectangle(0, 0, true).Width - 4; this.Controls[0].TextChanged += new EventHandler(MyDataGridView_TextChanged); } public string SearchText { get { return this.Controls[0].Text; } } private void MyDataGridView_TextChanged(object sender, EventArgs e) { this.DataSource = null; this.Rows.Clear(); this.Columns.Clear(); DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = "Search"; col.SortMode = DataGridViewColumnSortMode.NotSortable; this.Columns.Add(col); this.Rows.Add(); this.Controls.Add(new TextBox()); this.Controls[0].Top = this.GetCellDisplayRectangle(0, 0, true).Top + 2; this.Controls[0].Left = this.GetCellDisplayRectangle(0, 0, true).Left + 2; this.Controls[0].Width = this.GetCellDisplayRectangle(0, 0, true).Width - 4; this.Controls[0].TextChanged += new EventHandler(MyDataGridView_TextChanged); foreach (DataGridViewColumn column in this.Columns) { column.DataPropertyName = column.HeaderText; } // TODO: Perform search and update DataGridView with results } } } ``` MySearchControl.cs: ```csharp using System; using System.Windows.Forms; namespace MySearchControl { public partial class MySearchControl : UserControl { public MySearchControl() { InitializeComponent(); MyDataGridView dataGridView = new MyDataGridView(); dataGridView.Dock = DockStyle.Fill; dataGridView.SearchTextChanged += new EventHandler(MySearchControl_SearchTextChanged); this.Controls.Add(dataGridView); } public DataGridView DataGridView { get { return (DataGridView)this.Controls[0]; } } private void MySearchControl_SearchTextChanged(object sender, EventArgs e) { // TODO: Perform search and update DataGridView with results } private void btnSearch_Click(object sender, EventArgs e) { // TODO: Perform search using search text from DataGridView } } } ``` 在WinForm中添加MySearchControl控件: ```csharp MySearchControl.MySearchControl mySearchControl1 = new MySearchControl.MySearchControl(); mySearchControl1.Dock = DockStyle.Fill; this.Controls.Add(mySearchControl1); ```

相关推荐

最新推荐

recommend-type

WinForm中DataGridView折叠控件【超好看】

刚到一家新公司,领导下发任务要用cs系统做一个表格折叠显示,这真是把我难倒了,自己工作6年一直以来都是做BS的系统。这如果在BS里面那太简单了,JqGrid默认都自带,可是DataGridview不支持折叠啊。自己一点经验...
recommend-type

C#中改变DataGridView控件边框颜色的方法

主要介绍了C#中改变DataGridView控件边框颜色的方法,默认的DataGridView边框颜色很丑,本文用编程方法实现修改DataGridView边框颜色,需要的朋友可以参考下
recommend-type

C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法

主要介绍了C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法,本文通过Application.DoEvents()解决这个问题,并讲解了Application.DoEvents()的作用,需要的朋友可以参考下
recommend-type

C#基本控件用法(winform,原创)

近期学习C#的winform,整理的常用控件用法,入门级 包括button,datagridview,list,listview,checkBox,TreeGridView,MdiForm
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依