编写winform 自定义控件,控件具备输入搜索条件的功能同时具备显示搜索结果的功能,显示用的是DataGridView控件,搜索用的TextBox位于DataGridView控件的第一行中,收缩条件与DatagridView的列绑定
时间: 2024-02-01 14:14:51 浏览: 65
winform自定义控件
首先,创建一个新的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);
```
阅读全文