عمل 2 كومبوبكس متاثرين ببعضهما وهذا هو الكود
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ComboBox comboBox2;
public struct mycity
{
public int id;
public string name;
}
//int[] myIntArray = new int[5];
public mycity[] cityar= new mycity[10];
public int temp;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"",
""});
this.comboBox1.Location = new System.Drawing.Point(184, 96);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(208, 21);
this.comboBox1.TabIndex = 2;
this.comboBox1.Text = "comboBox1";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button2
//
this.button2.Location = new System.Drawing.Point(72, 296);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(296, 23);
this.button2.TabIndex = 3;
this.button2.Text = "next";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(88, 344);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(136, 40);
this.button3.TabIndex = 4;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// comboBox2
//
this.comboBox2.Location = new System.Drawing.Point(200, 176);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(176, 21);
this.comboBox2.TabIndex = 5;
this.comboBox2.Text = "comboBox2";
this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(216, 40);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 40);
this.label1.Name = "label1";
this.label1.TabIndex = 7;
this.label1.Text = "user name";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(480, 461);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
string strCon="User ID=sa;Initial Catalog=Northwind;Data Source=ayman;User ID=sa;password=;persist security info=False;Connection Lifetime=0;Pooling=true;Max Pool Size=10000;Min Pool Size = 40;";
SqlConnection Conn =new SqlConnection(strCon);
DataSet DS = new DataSet();
SqlDataAdapter adapter1 = new SqlDataAdapter();
string strSQL="Select * from categories";
adapter1.SelectCommand = new SqlCommand(strSQL,Conn);
adapter1.Fill(DS);
Conn.Open();
for (int i=0;i<DS.Tables[0].Rows.Count;i++)
{
// label1.Text +=DS.Tables[0].Rows["CategoryName"]+"\n";
}
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
// Form2 form=new Form2();
// form.Show();
// this.Hide();
}
private void Form1_Load(object sender, System.EventArgs e)
{
string strCon="User ID=sa;Initial Catalog=Northwind;Data Source=ayman;User ID=sa;password=;persist security info=False;Connection Lifetime=0;Pooling=true;Max Pool Size=10000;Min Pool Size = 40;";
SqlConnection Conn =new SqlConnection(strCon);
DataSet DS = new DataSet();
SqlDataAdapter adapter1 = new SqlDataAdapter();
string strSQL="Select * from city";
adapter1.SelectCommand = new SqlCommand(strSQL,Conn);
adapter1.Fill(DS);
Conn.Open();
for (int i=0;i<DS.Tables[0].Rows.Count;i++)
{
//comboBox1.Items=DS.Tables[0].Rows["CategoryName"];
this.comboBox1.Items.AddRange(new object[] {DS.Tables[0].Rows["idname"]});
this.cityar.id= Int32.Parse(DS.Tables[0].Rows["idcity"].ToString());
this.cityar.name = DS.Tables[0].Rows["idname"].ToString();
}
Conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
comboBox2.Items.Clear();
string strCon="User ID=sa;Initial Catalog=Northwind;Data Source=ayman;User ID=sa;password=;persist security info=False;Connection Lifetime=0;Pooling=true;Max Pool Size=10000;Min Pool Size = 40;";
SqlConnection Conn =new SqlConnection(strCon);
DataSet DS2 = new DataSet();
SqlDataAdapter adapter2 = new SqlDataAdapter();
///////////////get id from array (cityar)//////////////////////////
for(int i=0; i< 10; i++)
{
if(comboBox1.SelectedItem.ToString() == cityar.name)
{
temp = cityar.id;
// MessageBox.Show(cityar.id.ToString());
}
}
// MessageBox.Show(temp.ToString());
//////////////////////////////////////////////////////////////////
// MessageBox.Show(comboBox1.SelectedItem.ToString());
string strSQL="Select * from departs where idcity= '"+temp+"' ";
adapter2.SelectCommand = new SqlCommand(strSQL,Conn);
adapter2.Fill(DS2);
Conn.Open();
for (int i=0;i<DS2.Tables[0].Rows.Count;i++)
{
// MessageBox.Show(DS2.Tables[0].Rows["idcity"].ToString());
//if(comboBox2.SelectedItem == DS2.Tables[0].Rows["idcity"])
//this.comboBox2.Items.Add(new object[] {DS2.Tables[0].Rows["depnmn"]});
//comboBox1.Items=DS.Tables[0].Rows["CategoryName"];
this.comboBox2.Items.AddRange(new object[] {DS2.Tables[0].Rows["depnmn"]});
}
Conn.Close();
}//{
//string strCon="User ID=sa;Initial Catalog=Northwind;Data Source=ayman;User ID=sa;password=;persist security info=False;Connection Lifetime=0;Pooling=true;Max Pool Size=10000;Min Pool Size = 40;";
//SqlConnection Conn =new SqlConnection(strCon);
//DataSet DS = new DataSet();
//SqlDataAdapter adapter1 = new SqlDataAdapter();
//string strSQL="Select * from departs";
//adapter1.SelectCommand = new SqlCommand(strSQL,Conn);
//adapter1.Fill(DS);
//Conn.Open();
//for (int i=0;i<DS.Tables[0].Rows.Count;i++)
//{
// }
private void button2_Click(object sender, System.EventArgs e)
{
Form2 f1=new Form2();
f1.Show();
this.Hide();
}
private void button3_Click(object sender, System.EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}(h)