WinForm 2.0 textBox作为参数的查询,显示在DataGridView
.cs文件內容:
??? private void GetData()
??????? {
??????????? try
??????????? {
??????????????? // Specify a connection string. Replace the given value with a
??????????????? // valid connection string for a Northwind SQL Server sample
??????????????? // database accessible to your system.
??????????????? String connectionString =
??????????????????? "Integrated Security=SSPI;Persist Security Info=False;" +
??????????????????? "Initial Catalog=Northwind;Data Source=localhost";
??????????????? SqlConnection connection = new SqlConnection(connectionString);
??????????????? // Create a DataSet.
??????????????? DataSet data = new DataSet();
??????????????? data.Locale = System.Globalization.CultureInfo.InvariantCulture;
??????????????? // Add data from the Customers table to the DataSet.
??????????????? string sql = "select * from Customers where CustomerID = '"+this.textBox1.Text+"'";
??????????????? SqlDataAdapter masterDataAdapter = new
??????????????????? SqlDataAdapter(sql, connection);
??????????????? masterDataAdapter.Fill(data, "Customers");
??????????????? this.dataGridView1.DataSource = data.Tables[0].DefaultView;
??????????? }
??????????? catch (SqlException)
??????????? {
??????????????? MessageBox.Show("To run this example, replace the value of the " +
??????????????????? "connectionString variable with a connection string that is " +
??????????????????? "valid for your system.");
??????????? }
??????? }
??????? private void textBox1_TextChanged(object sender, EventArgs e)
??????? {
??????????? GetData();
??????? }
namespace WindowsApplication9
{
??? partial class Form1
??? {
??????? /// <summary>
??????? /// 必需的設計器變量。
??????? /// </summary>
??????? private System.ComponentModel.IContainer components = null;
??????? /// <summary>
??????? /// 清理所有正在使用的資源。
??????? /// </summary>
??????? /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
??????? protected override void Dispose(bool disposing)
??????? {
??????????? if (disposing && (components != null))
??????????? {
??????????????? components.Dispose();
??????????? }
??????????? base.Dispose(disposing);
??????? }
??????? #region Windows 窗體設計器生成的代碼
??????? /// <summary>
??????? /// 設計器支持所需的方法 - 不要
??????? /// 使用代碼編輯器修改此方法的內容。
??????? /// </summary>
??????? private void InitializeComponent()
??????? {
??????????? this.textBox1 = new System.Windows.Forms.TextBox();
??????????? this.dataGridView1 = new System.Windows.Forms.DataGridView();
??????????? ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
??????????? this.SuspendLayout();
??????????? //
??????????? // textBox1
??????????? //
??????????? this.textBox1.Location = new System.Drawing.Point(3, 3);
??????????? this.textBox1.Name = "textBox1";
??????????? this.textBox1.Size = new System.Drawing.Size(100, 21);
??????????? this.textBox1.TabIndex = 0;
??????????? this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
??????????? //
??????????? // dataGridView1
??????????? //
??????????? this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
??????????? this.dataGridView1.Location = new System.Drawing.Point(3, 30);
??????????? this.dataGridView1.Name = "dataGridView1";
??????????? this.dataGridView1.RowTemplate.Height = 23;
??????????? this.dataGridView1.Size = new System.Drawing.Size(409, 171);
??????????? this.dataGridView1.TabIndex = 1;
??????????? //
??????????? // Form1
??????????? //
??????????? this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
??????????? this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
??????????? this.ClientSize = new System.Drawing.Size(416, 206);
??????????? this.Controls.Add(this.dataGridView1);
??????????? this.Controls.Add(this.textBox1);
??????????? this.Name = "Form1";
??????????? this.Text = "Form1";
??????????? this.Load += new System.EventHandler(this.Form1_Load);
??????????? ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
??????????? this.ResumeLayout(false);
??????????? this.PerformLayout();
??????? }
??????? #endregion
??????? private System.Windows.Forms.TextBox textBox1;
??????? private System.Windows.Forms.DataGridView dataGridView1;
??? }
}
總結
以上是生活随笔為你收集整理的WinForm 2.0 textBox作为参数的查询,显示在DataGridView的全部內容,希望文章能夠幫你解決所遇到的問題。