2010-06-02 5 views
0

두 개의 폼과 클래스가 있는데 쿼리는 저장 프로 시저에서 반환됩니다. 난 그냥 페치 형태 2와 콤보 상자에 채워 참조 단지 채워지지 않은 특정 값을 얻을 응용 프로그램을 실행할 때콤보 상자의 데이터 바인딩

-- 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 

} 
} 

이미 디자이너, 를 통해 콤보 상자 항목을 바인더 제본했다. C#에 익숙해 져서 익숙해 지도록 도와주세요

+1

웹이나 윈폼 : 여기

은 예입니다? –

+0

질문을 더 명확하게 나타낼 수 있습니까? 나는 당신이 무엇을 요구하고 있는지 이해하는데 어려움을 겪고있다. 이것은 Windows 양식입니까 아니면 웹 응용 프로그램입니까? –

답변

0

this가 도움이되는지 확인하십시오.

2

가정 윈폼 ...

ComboBox 제어 세 가지 특성을 갖는다는 DataBinding을 사용하면서 사용한다.

  1. DataSource;
  2. DisplayMember;
  3. ValueMember.

데이터 소스

데이터 소스는 데이터베이스, 웹 서비스, 이상 데이터 바인딩 된 컨트롤을 생성하는 데 사용할 수있는 개체가 될 수 있습니다. DataSource 속성이 설정되면 항목 컬렉션을 수정할 수 없습니다.

DisplayMember

오브젝트의 다양한 유형을 표시 할 수 ListControl에서 상속 제어한다. 지정된 속성이 개체에 없거나 DisplayMember 값이 빈 문자열 ("") 인 경우 대신 개체의 ToString 메서드 결과가 표시됩니다.

새 디스플레이 멤버를 설정할 수 없으면 이전 디스플레이 멤버 설정이 유지됩니다. ValueMember

는 데이터를 결합하는 경우에 ValueMember 속성의 내용을 지정합니다.

속성을 빈 문자열 ("") 또는 null 참조 (Visual Basic의 경우 Nothing)로 설정하여 ValueMember 속성을 지울 수 있습니다.

새 ValueMember 속성을 설정하면 ValueMemberChanged 및 SelectedValueChanged 이벤트가 발생합니다.

지금, 당신의 저장 프로 시저의 결과는 IList, BindingList<T> 또는 기타 바인딩 컬렉션의 메모리에 저장받을 것이다.

이 컬렉션은 ComboBoxDataSource으로 설정해야합니다. 가장 좋은 방법은 BindingSource입니다.

public partial class Form1 : Form { 
    private BindingSource bindingSource1; 

    public Form1() { 
     InitializeComponent(); 
     bindingSource1 = new BindingSource(); 
     comboBox1.DataSource = bindingSource1; 
     comboBox1.DisplayMember = "PaymentMethod"; 
     comboBox1.ValueMember = "PaymentId"; 
     bindingSource1.DataSource = GetPayments(); // GetPayments() shall return one of the above-mentioned bindable collections of payments. 
    } 
} 
관련 문제