Winform 2.0 textbox as a parameter query, displayed in the datagridview

Source: Internet
Author: User

. CS file content:

Private void getdata ()
{
Try
{
// Specify a connection string. Replace the given value with
// 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 MERs where customerid = '" + this. textbox1.text + "'";
sqldataadapter masterdataadapter = new
sqldataadapter (SQL, connection);
masterdataadapter. fill (data, "customers");

This. datagridview1.datasource = data. tables [0]. defaultview;
}< br> catch (sqlexception)
{< br> MessageBox. show ("to run this example, replace the value of the" +
"connectionstring variable with a connection string that is" +
"valid for your system. ");
}< BR >}

Private void textbox1_textchanged (Object sender, eventargs E)
{
Getdata ();
}

Designer. CS File Content

Namespace windowsapplication9
{
Partial class form1
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. icontainer components = NULL;

///


/// clear all resources in use.
///
/// true if managed resources should be released; otherwise, false.
protected override void dispose (bool disposing)
{< br> If (disposing & (components! = NULL)
{< br> components. Dispose ();
}< br> base. Dispose (disposing);
}

# Region generated by Windows Form DesignerCode

/// <Summary>
/// The designer supports the required methods-do not
/// Use the code editor to modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. textbox1 = new system. Windows. Forms. Textbox ();
This. Maid ();
(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. textbox#textchanged );
//
// Datagridview1
//
This. Maid = system. Windows. Forms. Maid. autosize;
This. Maid = new system. Drawing. Point (3, 30 );
This. Maid = "maid ";
This. Maid. Height = 23;
This. datagridview1.size = new system. Drawing. Size (409,171 );
This. Maid = 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. form#load );
(System. componentmodel. isupportinitialize) (This. datagridview1). endinit ();
This. resumelayout (false );
This. initialize mlayout ();

}

# Endregion

private system. Windows. Forms. textbox textbox1;
private system. Windows. Forms. datagridview datagridatagri1;
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.