Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / October 2008

Tip: Looking for answers? Try searching our database.

Binding Custom Property on a textbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kilo - 08 Sep 2008 01:17 GMT
Dear All,

Is it possible to bind a gridcontrol and textbox to a
cutomized class.

Ex.

Class Employee
   Property Name
   Property Address

Bind to a grid control and textbox on a Form

Form 1
    DatagridView
    Textbox

How can I accomplish this using Visual Studio 2005?

Regards,

Cyp Drv.
Morten Wennevik [C# MVP] - 08 Sep 2008 07:06 GMT
> Dear All,
>
[quoted text clipped - 18 lines]
>
> Cyp Drv.

Hi Kilo,

You can use the same datasource to bind against multiple controls.

List<Employee> employees = new List<Employee>();
employees.Add(new Employee("Jim", "Seattle"));
employees.Add(new Employee("Bill", "New York"));
employees.Add(new Employee("Ann", "Florida"));

dataGridView1.DataSource = employees;
textBox1.DataBindings.Add("Text", employees, "Name");

Note, however, that you may need to use more suitable datasources than
simple lists.  Your best bet is to create a BindingSource of the list and
bind against this BindingSource.

List<Employee> employees = new List<Employee>();
employees.Add(new Employee("Jim", "Seattle"));
employees.Add(new Employee("Bill", "New York"));
employees.Add(new Employee("Ann", "Florida"));

BindingSource bs = new BindingSource(employees, "");
dataGridView1.DataSource = bs;
textBox1.DataBindings.Add("Text", bs, "Name");

When using databinding and you need to programmatically add and remove items
from a list, use a BindingList instead of List.  If you otherwise change
properties on bound objects, you may also need to use INotifyPropertyChanged
and fire off a PropertyChanged event.  

public class Employee : INotifyPropertyChanged
{
private string _name;
private string _address;
public string Name
{
   get { return _name; }
   set { _name = value; }
}

public string Address
{
   get { return _address; }
   set
   {
       if (_address != value)
       {
           _address = value;
           if (PropertyChanged != null)
               PropertyChanged(this, new
PropertyChangedEventArgs("Address"));
       }
   }
}

public Employee(string name, string address)
{
   Name = name;
   Address = address;
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Kilo - 05 Oct 2008 03:13 GMT
Dear Morten,

Thanks For your Reply.

Regards,

Kilo

>> Dear All,
>>
[quoted text clipped - 88 lines]
> #endregion
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.