Databinding in combo box
I have two forms, and a class, queries return in Stored procedure.
-- 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.ToStri开发者_运维问答ng());
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
}
}
I have already binded the combo box items through designer, When I run the application I just get the reference populated in form 2 and combo box just populated not the particular value which is fetched. New to c# so help me to get familiar
Assuming WinForms...
The ComboBox
control has three properties to be used while using DataBinding.
- DataSource;
- DisplayMember;
- ValueMember.
DataSource
A data source can be a database, a Web service, or an object that can later be used to generate data-bound controls. When the DataSource property is set, the items collection cannot be modified.
DisplayMember
The controls that inherit from ListControl can display diverse types of objects. If the specified property does not exist on the object or the value of DisplayMember is an empty string (""), the results of the object's ToString method are displayed instead.
If the new display member cannot be set, the previous display member setting is maintained.
ValueMember
Specify the contents of the ValueMember property in cases where you bind data.
You can clear the ValueMember property by setting the property to an empty string ("") or a null reference (Nothing in Visual Basic).
Setting a new ValueMember property raises the ValueMemberChanged and SelectedValueChanged events.
Now, the result of your stored procedure shall get stored in memory in an IList
, BindingList<T>
or any other bindable collection.
This collection should be set as the DataSource
of your ComboBox
. The best approach, in my opinion, is through a BindingSource
.
Here's an example:
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.
}
}
Check if this helps you.
精彩评论