有个小问题,请大家帮忙!

aqandy 2005-03-08 08:58:11

下面代码中“ComboBox”控件选项一改变,输入框内容也随之改变,主要是“ComboBox”并没有“SelectedIndexChanged”事件,这是什么原因???


/* This is the second version of "how to populate the data
* from a table in a ComboBox" program.
* here is used :
-TableMapping mechanism
which controls how data adapters copy tables and columns of data
from a physical data source to ADO.NET in-memory objects
-DataViewManager
which binds a control to more than one table of a DataSet


* H黶eyin Altindag 18.07.2004
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;


namespace HowToGetDataIntoComboBox2
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

private OleDbConnection myConn;
private OleDbDataAdapter dAdapter;
private DataViewManager dviewmanager;
private DataSet dset;

private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
fnGetConnectedToDatabase();
//
// TODO: Add any constructor code after InitializeComponent call
//
}

...全文
87 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
aqandy 2005-03-08
  • 打赏
  • 举报
回复
问题是不要写“selectCommitChange”就能才生这种效果,有兴趣朋友可以复制上面代码到编辑器看看

Night_Elf 2005-03-08
  • 打赏
  • 举报
回复
在selectCommitChange事件中写程序
aqandy 2005-03-08
  • 打赏
  • 举报
回复
/// <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.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.BackColor = System.Drawing.Color.Pink;
this.comboBox1.Location = new System.Drawing.Point(144, 52);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(144, 20);
this.comboBox1.TabIndex = 1;
this.comboBox1.Text = "-Select a StudentID-";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(144, 34);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 18);
this.label1.TabIndex = 5;
this.label1.Text = "Select a StudentID";
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.Color.White;
this.textBox1.Enabled = false;
this.textBox1.Location = new System.Drawing.Point(144, 129);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(77, 21);
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.Color.White;
this.textBox2.Enabled = false;
this.textBox2.Location = new System.Drawing.Point(144, 164);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(154, 21);
this.textBox2.TabIndex = 7;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.BackColor = System.Drawing.Color.White;
this.textBox3.Enabled = false;
this.textBox3.Location = new System.Drawing.Point(144, 198);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(173, 21);
this.textBox3.TabIndex = 8;
this.textBox3.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(38, 129);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(68, 17);
this.label2.TabIndex = 9;
this.label2.Text = "StudentID";
//
// label3
//
this.label3.Location = new System.Drawing.Point(38, 164);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(106, 17);
this.label3.TabIndex = 10;
this.label3.Text = "Student Subject";
//
// label4
//
this.label4.Location = new System.Drawing.Point(38, 198);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 17);
this.label4.TabIndex = 11;
this.label4.Text = "Student Name";
//
// label5
//
this.label5.Location = new System.Drawing.Point(19, 267);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(307, 17);
this.label5.TabIndex = 11;
this.label5.Text = "WS:Winter Semester SS:Summer Semester";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(128)));
this.ClientSize = new System.Drawing.Size(362, 291);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.label5);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "How to get data into a ComboBox 2 ";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}

private void fnGetConnectedToDatabase()
{
string conStr="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=..\\..\\studentDB.mdb";
try
{
//open the connection to the database
myConn = new OleDbConnection(conStr);
myConn.Open();
}
catch(OleDbException ex)
{
MessageBox.Show("Error in connection ..."+ex.Message);
}

string sqlStr ="SELECT * FROM studentTable;";

//Instantiate a DataAdapter by passing the sqlStr and Connection.
//now data is in raw form
dAdapter = new OleDbDataAdapter(sqlStr,myConn);

//Instantiate a DataSet
dset = new DataSet();

//Gets a collection that provides the master mapping
// between a source table and a DataTable
dAdapter.TableMappings.Add("Table", "studentTable");

/*A data adapter object utilizes the Fill method
to populate a DataSet or a DataTable object with data
retrieved by a SELECT command. */
dAdapter.Fill(dset);
//When binding a DataSet, .NET automatically uses the corresponding
//DataViewManager provided through the DataSet.DefaultViewManager property
this.dviewmanager=dset.DefaultViewManager;

this.comboBox1.DataSource=this.dviewmanager;
//display "studentTable.StudentID" in the ComboBox
this.comboBox1.DisplayMember="studentTable.StudentID";

//DataBinding for the TextBox controls
this.textBox1.DataBindings.Add("Text", this.dviewmanager,"studentTable.StudentID");
this.textBox2.DataBindings.Add("Text", this.dviewmanager, "studentTable.StudentSubject");
this.textBox3.DataBindings.Add("Text", this.dviewmanager, "studentTable.StudentName");

// Close the connection to the database.
this.myConn.Close();
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}

}
}

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧