填充组合框并在文本框中显示有错误 [英] Populating combobox and displayin in the text box has error
问题描述
填充combox并在组合框中显示值
例如如果我在组合框中选择名称,则相应的ID应显示在文本框中.有人知道吗?我正在学习C#.我尝试过可以让任何人检查以下代码.
阅读后,我尝试将其放入绑定列表数组中,然后我说
combobox1.datasorce =新变量
Pl.检查以下代码.
populating the combox and displaying the value in the combobox
for eg. if I choose name in the combobox the corresponding ID should display in the text box . Do anybody know? I am learning c#. I tried it can anybody examine the following code.
After reading I tried to put it in a Binding list array and then I said
combobox1.datasorce = the new variable
Pl. examine the below code.
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.OleDb;
namespace DBExample
{
public partial class Form1 : Form
{
private OleDbConnection dbConn; // Connectionn object
private OleDbCommand dbCmd; // Command object
private OleDbDataReader dbReader;// Data Reader object
private Member aMember;
private string sConnection;
// private TextBox tb1;
// private TextBox tb2;
private string sql;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
// Construct an object of the OleDbConnection
// class to store the connection string
// representing the type of data provider
// (database) and the source (actual db)
sConnection =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:member.mdb";
dbConn = new OleDbConnection(sConnection);
dbConn.Open();
// Construct an object of the OleDbCommand
// class to hold the SQL query. Tie the
// OleDbCommand object to the OleDbConnection
// object
sql = "Select * From memberTable Order " +
"By LastName , FirstName ";
dbCmd = new OleDbCommand();
dbCmd.CommandText = sql;
dbCmd.Connection = dbConn;
// Create a dbReader object
dbReader = dbCmd.ExecuteReader();
while (dbReader.Read())
{
aMember = new Member
(dbReader["FirstName"].ToString(),
dbReader["LastName"].ToString(),
dbReader["StudentId"].ToString(),
dbReader["PhoneNumber"].ToString());
// tb1.Text = dbReader["FirstName"].ToString();
// tb2.Text = dbReader["LastName"].ToString();
// tb1.Text = aMember.X().ToString();
//tb2.Text = aMember.Y(aMember.ID).ToString();
BindingList<Member> comboBoxList = new BindingList<Member>();
comboBox1.DataSource = comboBoxList;
comboBox1.DisplayMember = "FirstName";
comboBox1.ValueMember = "ID";
// this.comboBox1.Items.Add("aMember.FirstName.ToString()");
// this.listBox1.Items.Add(aMember.ToString());
// MessageBox.Show(aMember.ToString());
// Console.WriteLine(aMember.ToString());
}
dbReader.Close();
dbConn.Close();
}
catch (System.Exception exc)
{
MessageBox.Show("show" + exc);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// textBox2.DataBindings.Add("Text", aMember, "aMember.ID");
textBox2.Text = comboBox1.SelectedValue.ToString();
this.textBox1.Text = comboBox1.SelectedItem.ToString();
//int[] item = (int[])comboBox1.SelectedItem;
//textBox2.Text = string.Empty;
// foreach (int i in item)
// textBox2.Text += item.ToString();
}
private void DbGUI_Load(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
推荐答案
您应该像这样修改代码,
You should modify your code like this,
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > 0)
{
// textBox2.DataBindings.Add("Text", aMember, "aMember.ID");
textBox2.Text = comboBox1.SelectedValue.ToString();
this.textBox1.Text = comboBox1.SelectedItem.ToString();
//int[] item = (int[])comboBox1.SelectedItem;
//textBox2.Text = string.Empty;
// foreach (int i in item)
// textBox2.Text += item.ToString();
}
}
最好的问候,
Theingi Win
Best Regards,
Theingi Win
嗨
在while循环之前将其移动
BindingList<member> comboBoxList = new BindingList<member>();</member></member>
绑定列表是一个信号实例,无需在循环中一次又一次地实例化
在while循环中获取此
Hi
Move this before the while loop
BindingList<member> comboBoxList = new BindingList<member>();</member></member>
Binding list is a signle instance, no need to instantiate again and again inside the loop
inside the while loop get this
Member mem1 = new Member("Student A", "Student A", 1, "XXXXXXX");
Member mem2 = new Member("Student B", "Student B", 2, "XXXXXXX");
comboBoxList.Add(mem1);
comboBoxList.Add(mem2);
您必须使用成员填充绑定列表
然后在while循环之外将其分配给组合框
You have to populate the binding list with members
Then out side the while loop assign this to the combobox
comboBox1.DataSource = comboBoxList;
comboBox1.DisplayMember = "FirstName";
comboBox1.ValueMember = "ID";
然后在Selection Change Event
Then in the Selection Change Event
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Member mem=(Member)comboBox1.SelectedItem;
textBox1.Text = mem.FirstName;
}
您正在做的是创建绑定列表,但是该绑定列表为空.
What you are doing is creating a binding list, but that binding list is empty.
您好
请尝试以下代码块
Hi
Please try the below code block
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// textBox2.DataBindings.Add("Text", aMember, "aMember.ID");
textBox2.Text = comboBox1.SelectedValue.ToString();
this.textBox1.Text = comboBox1.Text;
//int[] item = (int[])comboBox1.SelectedItem;
//textBox2.Text = string.Empty;
// foreach (int i in item)
// textBox2.Text += item.ToString();
}
这篇关于填充组合框并在文本框中显示有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!