适用场景:查询条件为有限个且动态的,查询条件有值时考虑,无值时不考虑。
gitee地址:https://gitee.com/zhangyf6548/winfrom-scaffold
首先winform界面方便调试
namespace WinFormsApp1
{
partial class LandDataForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.label1 = new System.Windows.Forms.Label();
this.btn_Ok = new System.Windows.Forms.Button();
this.btn_Cancel = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.btnQuery = new System.Windows.Forms.Button();
this.txtNum = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.txtLocation = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(26, 15);
this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(54, 20);
this.label1.TabIndex = 0;
this.label1.Text = "编号:";
//
// btn_Ok
//
this.btn_Ok.Location = new System.Drawing.Point(351, 487);
this.btn_Ok.Margin = new System.Windows.Forms.Padding(2, 4, 2, 4);
this.btn_Ok.Name = "btn_Ok";
this.btn_Ok.Size = new System.Drawing.Size(96, 31);
this.btn_Ok.TabIndex = 5;
this.btn_Ok.Text = "确定";
this.btn_Ok.UseVisualStyleBackColor = true;
this.btn_Ok.Click += new System.EventHandler(this.btn_Ok_Click);
//
// btn_Cancel
//
this.btn_Cancel.Location = new System.Drawing.Point(486, 487);
this.btn_Cancel.Margin = new System.Windows.Forms.Padding(2, 4, 2, 4);
this.btn_Cancel.Name = "btn_Cancel";
this.btn_Cancel.Size = new System.Drawing.Size(96, 31);
this.btn_Cancel.TabIndex = 6;
this.btn_Cancel.Text = "取消";
this.btn_Cancel.UseVisualStyleBackColor = true;
this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToResizeColumns = false;
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.BackgroundColor = System.Drawing.Color.White;
this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView1.ColumnHeadersHeight = 29;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column4,
this.Column2,
this.Column3});
this.dataGridView1.Location = new System.Drawing.Point(30, 58);
this.dataGridView1.Margin = new System.Windows.Forms.Padding(2, 4, 2, 4);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.RowHeadersWidth = 51;
this.dataGridView1.RowTemplate.Height = 27;
this.dataGridView1.Size = new System.Drawing.Size(876, 383);
this.dataGridView1.TabIndex = 8;
this.dataGridView1.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dataGridView1_RowPostPaint);
//
// Column1
//
this.Column1.DataPropertyName = "index";
this.Column1.HeaderText = "序号";
this.Column1.MinimumWidth = 6;
this.Column1.Name = "Column1";
this.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.Column1.Width = 125;
//
// Column4
//
this.Column4.DataPropertyName = "num";
this.Column4.HeaderText = "编号";
this.Column4.MinimumWidth = 6;
this.Column4.Name = "Column4";
this.Column4.Width = 190;
//
// Column2
//
this.Column2.DataPropertyName = "name";
this.Column2.HeaderText = "名称";
this.Column2.MinimumWidth = 6;
this.Column2.Name = "Column2";
this.Column2.Width = 200;
//
// Column3
//
this.Column3.DataPropertyName = "location";
this.Column3.HeaderText = "位置";
this.Column3.MinimumWidth = 6;
this.Column3.Name = "Column3";
this.Column3.Width = 300;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(208, 15);
this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(54, 20);
this.label2.TabIndex = 9;
this.label2.Text = "名称:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(456, 15);
this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(54, 20);
this.label3.TabIndex = 10;
this.label3.Text = "位置:";
//
// btnQuery
//
this.btnQuery.Location = new System.Drawing.Point(810, 13);
this.btnQuery.Margin = new System.Windows.Forms.Padding(2, 4, 2, 4);
this.btnQuery.Name = "btnQuery";
this.btnQuery.Size = new System.Drawing.Size(96, 31);
this.btnQuery.TabIndex = 11;
this.btnQuery.Text = "查询";
this.btnQuery.UseVisualStyleBackColor = true;
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
//
// txtNum
//
this.txtNum.Location = new System.Drawing.Point(85, 13);
this.txtNum.Name = "txtNum";
this.txtNum.Size = new System.Drawing.Size(100, 27);
this.txtNum.TabIndex = 12;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(277, 13);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(152, 27);
this.txtName.TabIndex = 13;
//
// txtLocation
//
this.txtLocation.Location = new System.Drawing.Point(526, 13);
this.txtLocation.Name = "txtLocation";
this.txtLocation.Size = new System.Drawing.Size(170, 27);
this.txtLocation.TabIndex = 14;
//
// LandDataForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(928, 540);
this.Controls.Add(this.txtLocation);
this.Controls.Add(this.txtName);
this.Controls.Add(this.txtNum);
this.Controls.Add(this.btnQuery);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.btn_Cancel);
this.Controls.Add(this.btn_Ok);
this.Controls.Add(this.label1);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Margin = new System.Windows.Forms.Padding(2, 4, 2, 4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "LandDataForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "数据查询";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ImportExcelFile_FormClosing);
this.Load += new System.EventHandler(this.ImportExcelFile_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btn_Ok;
private System.Windows.Forms.Button btn_Cancel;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnQuery;
private System.Windows.Forms.TextBox txtNum;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtLocation;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Class;
namespace WinFormsApp1
{
public partial class LandDataForm : Form
{
//获取系统缩放比
public static List<LandData> selectDate;
List<LandData> LandDataAllList = new List<LandData>();
public LandDataForm()
{
InitializeComponent();
}
private void btnQuery_Click(object sender, EventArgs e)
{
#region
string num = txtNum.Text == null ? string.Empty : txtNum.Text.Trim();
string name = txtName.Text == null ? string.Empty : txtName.Text.Trim();
string location = txtLocation.Text == null ? string.Empty : txtLocation.Text.Trim();
//声明查询参数
Func<LandData, bool> filterNum = x => true, filterName = x => true, filterLocation = x => true;
//设置查询条件
filterNum = x => x.num == num;
filterName = x => x.name == name;
filterLocation = x => x.location.Contains(location);//模糊查询
var result = LandDataAllList;
//查询
if (!string.IsNullOrEmpty(num))
{
result = result.Where(filterNum).ToList();
}
if (!string.IsNullOrEmpty(name))
{
result = result.Where(filterName).ToList();
}
if (!string.IsNullOrEmpty(location))
{
result = result.Where(filterLocation).ToList();
}
dataGridView1.DataSource = result;
if (result.Count > 0)
{
MessageBox.Show("查询结果成功");
}
else
{
MessageBox.Show("查询结果为空");
}
#endregion
}
private void btn_Ok_Click(object sender, EventArgs e)
{
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void ImportExcelFile_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void ImportExcelFile_Load(object sender, EventArgs e)
{
Color rblue = ColorTranslator.FromHtml("#3A80EC");
btn_Ok.FlatStyle = FlatStyle.Flat;
btn_Ok.BackColor = rblue;
btn_Ok.FlatAppearance.BorderColor = rblue;
btn_Ok.ForeColor = Color.White;
btn_Cancel.FlatStyle = FlatStyle.Flat;
btn_Cancel.ForeColor = rblue;
btn_Cancel.FlatAppearance.BorderColor = rblue;
for (int i = 0; i < 30; i++)
{
LandDataAllList.Add(new LandData("num" + i, "name" + i, "位置" + i));
}
btnQuery_Click(null, null);
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[0].Value = row.Index + 1;
}
}
}
}
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="openFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="Column1.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Column4.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Column2.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Column3.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>