| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1354 人关注过本帖
标题:[讨论]关于comboBox的问题
只看楼主 加入收藏
zhouhua1982
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2005-12-25
收藏
得分:0 

不知道这是不是你要的。写的简单。。详细的您自己改吧
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.ComboBox comboBox1;

private string connectionstring=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Directory.GetCurrentDirectory()+"\\db\\class.mdb;";
private System.Data.OleDb.OleDbConnection myconn;
private System.Data.OleDb.OleDbCommandBuilder mybld;
private System.Data.OleDb.OleDbDataAdapter myada;//
private DataSet ds=new DataSet();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.comboBox1 = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(212, 248);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "显示";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(44, 80);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(432, 136);
this.dataGrid1.TabIndex = 4;
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(172, 32);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(160, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.TextChanged += new System.EventHandler(this.comboBox1_TextChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 349);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public DataTable selectdatabase(string tempstrole)//用与打开数据库并查找相应表
{
this.myconn=new OleDbConnection(connectionstring);
DataSet tempDataSet=new DataSet();
try
{

this.myada=new OleDbDataAdapter(tempstrole,myconn);
myada.Fill(tempDataSet);

}
catch(Exception err)
{
MessageBox.Show(err.ToString());

}
return tempDataSet.Tables[0];
}

private void Form1_Load(object sender, System.EventArgs e)//当窗体加载时候把第一个表的班级信息添加到combobox选项里
{
string selone="select 班级 from 第一个表";
DataTable comtab=this.selectdatabase(selone);
if(comtab.Rows.Count!=0)
{
for(int i=0;i<comtab.Rows.Count;i++)
{
this.comboBox1.Items.Add(comtab.Rows[i]["班级"]);
}

}
else
{
MessageBox.Show("在第一个表中没有找到班级信息");
}

}

private void button1_Click(object sender, System.EventArgs e)
{
string btnsel="select * from 第二个表 where 班级='"+this.comboBox1.Text.Trim()+"'";

DataTable btntable=this.selectdatabase(btnsel);
this.dataGrid1.DataSource=btntable;//点击buttons按钮时候显示将第二个表里相应的课程学生显示到datagrid里
}

private void comboBox1_TextChanged(object sender, System.EventArgs e)
{
// string btnsel="select * from 第二个表 where 班级='"+this.comboBox1.Text.Trim()+"'";
//
// DataTable btntable=this.selectdatabase(btnsel);
// this.dataGrid1.DataSource=btntable;

}//当选中combobox中某一itms时候在datagrid里显示用到 combobox的 textchanged事件
}
}

[此贴子已经被作者于2006-1-4 19:11:39编辑过]


刚毕业,寻找延吉(延边大学)大2或大3喜欢学习的朋友您大学学到什么了?? qq:109489889
2006-01-04 19:08
zhouhua1982
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2005-12-25
收藏
得分:0 
程序下载


刚毕业,寻找延吉(延边大学)大2或大3喜欢学习的朋友您大学学到什么了?? qq:109489889
2006-01-04 19:10
zhouhua1982
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2005-12-25
收藏
得分:0 
程序下载
YumU8QK1.rar (34.08 KB) [讨论]关于comboBox的问题



刚毕业,寻找延吉(延边大学)大2或大3喜欢学习的朋友您大学学到什么了?? qq:109489889
2006-01-04 19:11
快速回复:[讨论]关于comboBox的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021092 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved