我问个简单的问题
我有个comboBox 当我点击向下三角形的时候显示我数据库表中所有的记录(主要用于浏览)
数据库名 outin 表名guest(有多条记录) 当我选中一条记录 就再把他绑定到DataGrid里面
代码怎么写
[此贴子已经被作者于2005-12-31 12:23:38编辑过]
我简单写了一个。。改变combobox中内容datagrid 里显示不同信息不知道是不是你需要的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form//
{
private System.Windows.Forms.ComboBox comboBox1;//下拉框 在items中填写表名信息。。与数据库中要显示的表名要相同
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;//点击此按钮时候datagrid1中显示相应表信息
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.comboBox1 = new System.Windows.Forms.ComboBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"第一个表",
"第二个表"});
this.comboBox1.Location = new System.Drawing.Point(136, 32);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(160, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 80);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(432, 136);
this.dataGrid1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(176, 248);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "显示";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 309);
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());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
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 button1_Click(object sender, System.EventArgs e)//点击button1按钮时候发生
{
DataTable grdtable=new DataTable();
string sel="select * from "+this.comboBox1.Text;
grdtable=this.selectdatabase(sel);
this.dataGrid1.DataSource=grdtable;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)//当选中combobox中某条信息时候发生。
{
DataTable grdtable=new DataTable();
string sel="select * from "+this.comboBox1.Text;
grdtable=this.selectdatabase(sel);
this.dataGrid1.DataSource=grdtable;
}
}
}