Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the example below, I fill two labels white SQL data.

This works well! I asked myself, just wondering if it is also possible to binding info1 and info2 in a single label, for example, load in label1?


If (reader.Read())
{
Label1.text = convert.ToString(reader["info1"]);
Label2.text = convert.ToString(reader["info2"]);

}
Posted

You can do that
C#
Label1.text = Convert.ToString(reader["info1"])+"-"+Convert.ToString(reader["info2"]);

OR
C#
Label1.text = convert.ToString(reader["info1"]);
Label1.text += convert.ToString(reader["info2"]);
 
Share this answer
 
Comments
MaikelO1 24-Nov-15 5:03am    
Tnxs!
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace YourNamespace
{
    public class BindLabel : INotifyPropertyChanged
    {
        private string _labelText;

        // for testing only
        private Dictionary<string,> reader = new Dictionary<string,>
        {
            {"info1",99}, {"info2",34}
        }; 

        public string LabelText
        {
            get { return _labelText; }
            set
            {
                _labelText =
                    string.Format("{0} : {1}",
                        Convert.ToString(reader["info1"]),
                        Convert.ToString(reader["info2"]));

                OnPropertyChanged(new PropertyChangedEventArgs("LabelText"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }
    }
}
Usage:
BindLabel bindingSource = new BindLabel();
 label1.DataBindings.Add("Text",bindingSource,"LabelText");
 label1.Text = "IgnoreThis"; 
The question is: do you really want to write such code for a simple string concatenation to be handled through binding ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900