如何使相关的组合框正常工作

时间:2016-05-24 12:27:36

标签: c# .net visual-studio combobox

我有两个组合框"年" &安培; "量"在它们的顶部,我确实获得了用户信息的值,因为在使用用户ID文本框调用时,有文本框填充了正确的数据。

两个组合框也填充了正确的数据,但我必须手动选择年份和相应的数量。

当我拨打数据时,我需要帮助#34;年" &安培; "量"应该在组合框中显示。当我选择一年时,金额应该相应地改变。最后但并非最不重要的是我的重置不是清除组合框。

using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace dss
{
    public partial class Form1 : Form

    {
        SqlConnection con = new SqlConnection("Data Source=USER-PC\\sqlexpress;Initial Catalog=JG_Test;Integrated Security=True");

        public Form1()
        {
            InitializeComponent();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            cmbYear.Items.Clear();
            string sql = "";
            con.Open();
            SqlCommand cmd = new SqlCommand();

            try
            {
                sql += "SELECT m.MemberId, m.Name, m.Address, m.Cellular, m.Email, p.PaymentId, p.Year, p.Amount from Members as m";
                sql += " INNER JOIN Payments as p ON m.MemberId = p.MemberId";
                sql += " WHERE m.MemberId = '" + tbID.Text + "' ORDER BY p.Year ASC";

            cmd.Connection = con;
            cmd.CommandText = sql;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                con.Close();
                if(dt.Rows.Count >0)
                {
                    for(int i = 0; i<=dt.Rows.Count -1;i++)
                    {
                        tbID.Text = dt.Rows[i]["MemberId"].ToString();
                        tbName.Text = dt.Rows[i]["Name"].ToString();
                        tbCellular.Text = dt.Rows[i]["Cellular"].ToString();
                        tbEmail.Text = dt.Rows[i]["Email"].ToString();
                        tbAddress.Text = dt.Rows[i]["Address"].ToString();

                        cmbAmount.Items.Add(dt.Rows[i]["Amount"].ToString());
                        cmbYear.Items.Add(dt.Rows[i]["Year"].ToString());

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        //This part displaying og the existing data from all the fileds corrssponding within the database//
        private void btnAdd_Click(object sender, EventArgs e)
        {
            {
                con.Open();

                string Sql = "INSERT INTO Members ( MemberId, Name, Cellular, Email, Address ) VALUES " + " (@Id, @name, @cell, @email, @address)";

                using (SqlCommand cmd = new SqlCommand(Sql, con))

                {
                    cmd.CommandText = Sql;
                    cmd.Parameters.AddWithValue("@Id", tbID.Text);
                    cmd.Parameters.AddWithValue("@name", tbName.Text);
                    cmd.Parameters.AddWithValue("@cell", tbCellular.Text);
                    cmd.Parameters.AddWithValue("@email", tbCellular.Text);
                    cmd.Parameters.AddWithValue("@address", tbAddress.Text);
                    cmd.ExecuteNonQuery();

                    Sql = "INSERT INTO Payments ( MemberId, [Year], [Amount] ) VALUES " + " (@Id, Amount, Year)";

                    cmd.Parameters.Clear();
                    cmd.CommandText = Sql;
                    cmd.Parameters.AddWithValue("@Id", tbID.Text);
                    cmd.Parameters.AddWithValue("@year", cmbYear.Text);
                    cmd.Parameters.AddWithValue("@amount", cmbAmount.Text);
                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Data Added");

                    tbID.Clear(); tbName.Clear(); tbCellular.Clear(); tbEmail.Clear(); tbAddress.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();
                    con.Close();
                }
            }
        }

        //This part represents adding of  new input data from all the fileds into the database//
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                string Sql = "UPDATE Members SET MemberId = '" + tbID.Text + "', Name = '" + tbName.Text + "', Cellular = '" + tbCellular.Text + "', Email = '" + tbEmail.Text + "', Address = '" + tbAddress.Text + "' WHERE MemberId = '" + tbID.Text + "' ";
                cmd.CommandText = Sql;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                Sql = "UPDATE Payments SET MemberId = '" + tbID.Text + "', Year = '" + cmbYear.Text + "', Amount = '" + cmbAmount.Text + "' WHERE MemberId = '" + tbID.Text + "' ";
                cmd.CommandText = Sql;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Data Updated");
                tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbCellular.Clear(); tbEmail.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();

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

        //This part represents deleteing of input data from all the fileds into the database//
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                string Sql = "DELETE FROM Members WHERE MemberId = '" + tbID.Text + "' ";
                cmd.CommandText = Sql;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                Sql = "DELETE  FROM Payments WHERE MemberId = '" + tbID.Text + "' ";
                cmd.CommandText = Sql;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbCellular.Clear(); tbEmail.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();
                MessageBox.Show("Data Deleted");

                con.Close();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }

        }

        //This part represents clearing of input data from all the fileds//
        private void btnReset_Click(object sender, EventArgs e)
        {
            tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbCellular.Clear(); tbEmail.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();
        }

        //This part represents shuting down the application//
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        }
    }

1 个答案:

答案 0 :(得分:0)

我倾向于简化一些事情。将个人数据和财务数据视为两部分。

首先,请求个人数据 - 保持简单

private void btnSearch_Click(object sender, EventArgs e)
{
string sql = "SELECT MemberId, Name, Address, Cellular, Email FROM Members WHERE MemberId = @Id";

SqlConnection con = new SqlConnection("myconnectionstring");
SqlCommand cmd = new SqlCommand(sql,con);

cmd.Parameters.Add("@Id",SqlDbType.Int).Value = tbID.Text;

DataTable dt = new DataTable();

try
{
    con.Open();
    dt.Load(cmd.ExecuteReader());
    con.Close();
}
catch (Exception ex)
{
con.Close();
Console.WriteLine(ex.Message);

}

tbID.Text = dt.Rows[0]["MemberId"].ToString();
tbName.Text = dt.Rows[0]["Name"].ToString();
tbCellular.Text = dt.Rows[0]["Cellular"].ToString();
tbEmail.Text = dt.Rows[0]["Email"].ToString();
tbAddress.Text = dt.Rows[0]["Address"].ToString();
}

一旦完成,转到第二部分 - 年/金额组合(几乎相同的代码)

string sql = "SELECT Year, Amount FROM Payments WHERE MemberId = @Id"

SqlConnection con = new SqlConnection("myconnectionstring");
SqlCommand cmd = new SqlCommand(sql,con);

cmd.Parameters.Add("@Id",SqlDbType.Int).Value = tbID.Text;

DataTable dt = new DataTable();

try
{
    con.Open();
    dt.Load(cmd.ExecuteReader());
    con.Close();
}
catch (Exception ex)
{
con.Close();
Console.WriteLine(ex.Message);

}

cmbYear.DataSource = dt;
cmbYear.DisplayMember = "Year";
cmbYear.ValueMember = "Amount";

最后,使用

告诉文本框需要阅读的内容
private void cmbYear_SelectionChangeCommitted(object sender, EventArgs e)
{

    amountTxt.Text = cmbYear.SelectedValue.ToString();

}
在组合框的SelectionChangeCommitted事件中

这应该让你排序!