首先我想道歉,因爲我很新的C#。這可能聽起來像一個愚蠢的問題;然而,我一直無法在互聯網上的任何地方或在C#上購買的幾本書中找到任何示例。我有一個窗體可以從SQL數據庫中獲取相當多的數據。第一個和最後一個名字被插入到兩個獨立的組合框中,一個將顯示信息爲「First Last」和另一個「Last,First」,以便於瀏覽名稱。組合框項目到多個文本框
由於一些幫助在這裏一天,我學會了如何填充一個文本框與組合框的選擇值。再次感謝您的幫助。
我需要做的是當從下拉框中選擇一個用戶,所有用戶的信息將在窗體上某些文本框內顯示。例子是,
LastName = textBox1
FirstName = textBox2
MiddleName = textBox3
我是否需要寫一個單獨的字符串,其中的每個字段,或者可以將數據從該查詢所有數據的SQL數據庫一根弦拉?
您提供的任何援助將不勝感激。
這裏是我迄今爲止
enter code here
namespace Tempus.Menus
{
public partial class Employees : Form
{
public Employees()
{
InitializeComponent();
//Connect to database for Employees Table Headers
SqlConnection myConnection = new SqlConnection(@"Server=Server4\INSTANCE;Integrated Security=true;" +
"user id=userID;password=password;" +
"Trusted_Connection=yes;" +
"Database=Database;" +
"connection timeout=30");
try
{
myConnection.Open();
string SqlDataPull = String.Format("SELECT * FROM Employees WHERE Lname IS NOT NULL {0}", (checkBox1.Checked ? "AND Active='Y'" : ""));
//string SqlDataPull2 = String.Format("SELECT * FROM Employees WHERE Fname IS NOT NULL {0} ORDER By Fname", (checkBox1.Checked ? "AND Active='Y'" : ""));
SqlCommand cmd = new SqlCommand(SqlDataPull, myConnection);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string strEmployee = String.Format("{0} {1}", dr["Fname"], dr["Lname"], dr["Mname"], dr["DOH"], dr["DOT"], dr["Active"], dr["DoNotRehireReason"], dr["PTO-balance"], dr["SNP-balance"], dr["Cred"]);
comboBox1.Items.Add(strEmployee);
string strEmployee2 = String.Format("{0}, {1}", dr["Lname"], dr["Fname"]);
comboBox2.Items.Add(strEmployee2);
int Fname = dr.GetInt32(0);
string firstName = String.Format("{0}", dr["Fname"]);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
if (myConnection != null)
{
myConnection.Dispose();
}
}
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1)
{
textBox1.Text = string.Empty;
}
else
{
textBox1.Text = comboBox1.SelectedItem.ToString();
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedIndex == -1)
{
textBox1.Text = string.Empty;
}
else
{
textBox1.Text = comboBox2.SelectedItem.ToString();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Employees_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Main myNewForm = new Main();
myNewForm.Show();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Reports myNewForm = new Reports();
myNewForm.Show();
this.Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void textBox8_TextChanged(object sender, EventArgs e)
{
}
private void textBox9_TextChanged(object sender, EventArgs e)
{
}
private void textBox7_TextChanged(object sender, EventArgs e)
{
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
// logic here for if the box has now been checked
}
else
{
// what to do if the box has been unchecked
}
}
}
}
@codingbiz:OK,林」有麻煩複制和粘貼我的代碼在此評論框.. – 2012-07-23 18:40:49
不要粘貼註釋。編輯原始問題。 – banging 2012-07-23 18:44:45
請將您的UI邏輯與控制器邏輯分開。將它們寫入兩個不同的類 – nawfal 2012-07-23 19:25:12