C # Method of using ComboBox control to realize the linkage effect between province and city

Source: Internet
Author: User

This paper describes the method of C # using ComboBox control to realize the linkage effect between province and city. Share to everyone for your reference. The implementation method is as follows:


The code is as follows:

Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Linq;

Using System.Text;

Using System.Windows.Forms;

Using System.Data.SqlClient;

Using System.Configuration;

namespace provinces and cities linkage

{

public partial class Form1:form

{

Public Form1 ()

{

InitializeComponent ();

}

private void Form1_Load (object sender, EventArgs e)

{

tsql.getconnection ();

using (SqlConnection conn = new SqlConnection (Tsql.getconnection ()))

{

Conn. Open ();

using (SqlCommand cmd = conn. CreateCommand ())

{

Cmd.commandtext = "SELECT * from Promary";

using (SqlDataReader dr = cmd. ExecuteReader ())

{

while (Dr. Read ())

{

Province PR = new province ();

Pr.proid = Dr. GetInt32 (Dr. GetOrdinal ("Proid")); //here to pay special attention to the type of database field proid in the database is the int type

Pr.proname = Dr. GetString (Dr. GetOrdinal ("Proname"));

ComboBox1.Items.Add (pr);//PR is a province object. Province has a proname and proid attribute. I filled an object into the ComboBox1. Then let the ComboBox1 DisplayMember property set Proname. "That is, let ComboBox1 display the Proname attribute of the PR object." So when I fill the city city, I can find the current selection Combobox1.selecteditem (or the current Selection object) corresponding to the Proid

}

}

}

Combobox1.selectedindex = 0; //Set the default option for ComcomboBox1 to 0

}

}

private void Combobox2_selectedindexchanged (object sender, EventArgs e)

{

}

private void ComboBox1_SelectedIndexChanged (object sender, EventArgs e)

{

ComboBox2.Items.Clear (); In the choice of time to clear the combox1 inside so that the item, in order to avoid the replacement of the province name, and the previous corresponding province name of the city did not delete.

Province tem = (province) Combobox1.selecteditem; Because the object is added to the COMBOBOX1.ITEMS.ADD (PR). So here's the ComBox1 the current object selected is assigned to a TEM object (there are two properties Proname and proid below the TEM object)

int id = tem.proid; Assigns the proid of the currently selected object to the ID, using the following SQL query statement

Tsql.getconnection ();//Gets the database connection string.

using (SqlConnection conn = new SqlConnection (Tsql.getconnection ()))

{

Conn. Open ();

using (SqlCommand cmd = conn. CreateCommand ())

{

Cmd.commandtext = "SELECT * from the city where [email protected]";

Cmd. Parameters.Add (New SqlParameter ("id", id));

using (SqlDataReader dr = cmd. ExecuteReader ())

{

while (Dr. Read ())

{

COMBOBOX2.ITEMS.ADD (Dr. GetString (Dr. GetOrdinal ("CityName"));

}

}

}

Combobox2.selectedindex = 0; Set the default option for ComcomboBox1 to 0

}

}

}

Class Tsql

{

public static string getconnection ()//database connection string

{

String getconn = configurationmanager.connectionstrings["Getconn"]. ConnectionString;

return getconn;

}

}

Class Province

{

public string Proname {get; set;}

public int proid {get; set;}

}

}


The code is as follows:

Using System.Data.SqlClient;

Namespace Provinces Selection 2

{

public partial class Form1:form

{

PublicForm1 ()

{

InitializeComponent ();

}

Privatevoid Form1_Load (Objectsender,eventargs e)

{

/*

Test code: It's OK to execute this code, Tom. This value is added to the comboBox1.

People p1 = new people ();

P1.name = "Tom";

P1.age = 25;

COMBOBOX1.ITEMS.ADD (P1.name);

*/

using (SqlConnection conn =newsqlconnection ("Data source= Phan-vaio;initialcatalog=dbpromary;integrated Security=true" ))

{

Conn. Open ();

using (SqlCommand cmd = conn. CreateCommand ())

{

Cmd.commandtext = "SELECT * from Promary";

using (SqlDataReader reader = cmd. ExecuteReader ())

{

while (reader. Read ())

{

String str =reader. GetString (reader. GetOrdinal ("Proname"));

COMBOBOX1.ITEMS.ADD (str);

Province PV =new Province ();

Set the DisplayMember property of the ComboBox1 control to "Provincename" to display the value of the Provincename property in the ComboBox1 control

Combobox1.displaymember = "Provincename";

Pv. Provincename =reader. GetString (reader. GetOrdinal ("Proname"));

Pv. Provinceid =reader. GetInt32 (reader. GetOrdinal ("proid"));

Although PV belongs to an object and adds an object to ComboxBox1, the control displays the value of the object's class name, but at the front combobox1.displaymember = "Provincename" The control is set to display the The Provincename value of the province class, so the Provincename value of the PV object is added.

COMBOBOX1.ITEMS.ADD (PV);

}

}

}

}

}

Privatevoid combobox1_selectedindexchanged (Object Sender,eventargse)//Note This is ComboBox1 not comboBox2

{

Try

{

Province tem = (province) Combobox1.selecteditem;

int id = tem. Provinceid;

ComboBox2.Items.Clear ();

using (SqlConnection conn =newsqlconnection ("Data source= Phan-vaio;initialcatalog=dbpromary;integrated Security=true" ))

{

Conn. Open ();

using (SqlCommand cmd = conn. CreateCommand ())

{

Query the city (cities) table for Proid and promary (province) Table proid all the same data.

Cmd.commandtext = "SELECT * from the city where proid [email protected]";

Cmd. Parameters.Add (New SqlParameter ("id", id));

SqlDataReader dr = cmd. ExecuteReader ();

while (Dr. Read ())

{

COMBOBOX2.ITEMS.ADD (Dr. GetString (Dr. GetOrdinal ("CityName"));

}

}

}

}

catch (Exception ex)

{

MessageBox.Show ("error message:" + ex.) Message);

}

}

}

Class Province

{

public string Provincename {get;set;}

public int Provinceid {get;set;}

}

class people

//{

public string name {get; set;}

public int Age {get; set;}

//}

}

As shown in the following:

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Method of using ComboBox control to realize the linkage effect between province and city

This address: http://www.paobuke.com/develop/c-develop/pbk23162.html






Related Content C # WinForm implementing a control on a form free drag function Example C # textbox implementation of input hint function C # read the system font color and size enumeration in C # (plain and easy to understand)
C # uses OLE DB to read Excel table content to a DataTable method C # using a foreach statement to traverse a queue (queued) methods of dotnetcharting in C # How to implement custom timing components in C #

C # Method of using ComboBox control to realize the linkage effect between province and city

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.