组合框中的数据绑定

时间:2010-06-02 14:55:28

标签: c# sql

我有两个表单,一个类,查询在存储过程中返回。

-- Stored Procedure:

ALTER PROCEDURE [dbo].[Payment_Join]
    @reference nvarchar(20)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT p.iPaymentID
            , p.nvReference
            , pt.nvPaymentType
            , p.iAmount
            , m.nvMethod
            , u.nvUsers
            , p.tUpdateTime
        FROM Payment p
            , tblPaymentType pt
            , tblPaymentMethod m
            , tblUsers u
        WHERE p.nvReference = @reference
            and p.iPaymentTypeID = pt.iPaymentTypeID
            and p.iMethodID = m.iMethodID
            and p.iUsersID = u.iUsersID 
END

// payment.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Finance {

class payment {

    string connection = global::Finance.Properties.Settings.Default.PaymentConnectionString;

    #region Fields

    int _paymentid = 0;
    string _reference = string.Empty;
    string  _paymenttype;
    double _amount = 0;
    string _paymentmethod;
    string _employeename;
    DateTime _updatetime = DateTime.Now;

    #endregion

    #region Properties

    public int paymentid
    {
        get { return _paymentid; }
        set { _paymentid = value; }
    }
    public string reference
    {
        get { return _reference; }
        set { _reference = value; }

    }
    public string paymenttype
    {
        get { return _paymenttype; }
        set { _paymenttype = value; }
    }
    public string paymentmethod
    {
        get { return _paymentmethod; }
        set { _paymentmethod = value; }
    }
    public double amount
    {
        get { return _amount;}
        set { _amount = value; }
    }
    public string employeename
    {
        get { return _employeename; }
        set { _employeename = value; }
    }
    public DateTime updatetime
    {
        get { return _updatetime; }
        set { _updatetime = value; }
    }
    #endregion

    #region Constructor 

    public payment()
    {
    }

    public payment(string refer)
    {
        reference = refer;

    }
    public payment(int paymentID, string Reference, string Paymenttype, double Amount, string Paymentmethod, string Employeename, DateTime Time)
    {
        paymentid = paymentID;
        reference = Reference;
        paymenttype = Paymenttype;
        amount = Amount;
        paymentmethod = Paymentmethod;
        employeename = Employeename;
        updatetime = Time;

    }
    #endregion



    #region Methods

    public void Save()
    {
        try
        {
            SqlConnection connect = new SqlConnection(connection);
            SqlCommand command = new SqlCommand("payment_create", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@reference", reference));
            command.Parameters.Add(new SqlParameter("@paymenttype", paymenttype));
            command.Parameters.Add(new SqlParameter("@amount", amount));
            command.Parameters.Add(new SqlParameter("@paymentmethod", paymentmethod));
            command.Parameters.Add(new SqlParameter("@employeename", employeename));
            command.Parameters.Add(new SqlParameter("@updatetime", updatetime));
            connect.Open();
            command.ExecuteScalar();
            connect.Close();

        }
        catch
        {

        }
    }

    public void Load(string reference)
    {
        try
        {


            SqlConnection connect = new SqlConnection(connection);
            SqlCommand command = new SqlCommand("Payment_Join", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@Reference", reference));

            //MessageBox.Show("ref = " + reference);

            connect.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {



                this.reference = Convert.ToString(reader["nvReference"]);
                // MessageBox.Show(reference);
                // MessageBox.Show("here");

                // MessageBox.Show("payment type id = " + reader["nvPaymentType"]);

                // MessageBox.Show("here1");


                this.paymenttype = Convert.ToString(reader["nvPaymentType"]);

                // MessageBox.Show(paymenttype.ToString());
                this.amount = Convert.ToDouble(reader["iAmount"]);
                this.paymentmethod = Convert.ToString(reader["nvMethod"]);
                this.employeename = Convert.ToString(reader["nvUsers"]);
                this.updatetime = Convert.ToDateTime(reader["tUpdateTime"]);


            }
            reader.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Check it again" + ex);
        }
    }

    #endregion

}
}

我已经通过设计师绑定了组合框项目, 当我运行应用程序时,我只是在表单2中填充了引用,并且刚刚填充的组合框不是获取的特定值。 c#的新手让我熟悉

2 个答案:

答案 0 :(得分:2)

假设WinForms ......

ComboBox控件在使用DataBinding时有三个属性。

  1. DataSource;
  2. DisplayMember;
  3. ValueMember
  4. <强>数据源

      

    数据源可以是数据库,Web服务或稍后可用于生成数据绑定控件的对象。设置DataSource属性后,无法修改items集合。

    <强> DisplayMember

      

    从ListControl继承的控件可以显示不同类型的对象。如果对象上不存在指定的属性,或者DisplayMember的值为空字符串(“”),则会显示对象的ToString方法的结果。

         

    如果无法设置新的显示成员,则会保留先前的显示成员设置。

    <强> ValueMember

      

    在绑定数据的情况下指定ValueMember属性的内容。

         

    您可以通过将属性设置为空字符串(“”)或空引用(在Visual Basic中为Nothing)来清除ValueMember属性。

         

    设置新的ValueMember属性会引发ValueMemberChanged和SelectedValueChanged事件。

    现在,存储过程的结果将存储在IListBindingList<T>或任何其他可绑定集合的内存中。

    此集合应设置为DataSource的{​​{1}}。在我看来,最好的方法是通过BindingSource

    以下是一个例子:

    ComboBox

答案 1 :(得分:0)

检查this是否对您有所帮助。