Select row on DataGridView and display info in other controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Herumezu
    New Member
    • Oct 2008
    • 1

    Select row on DataGridView and display info in other controls

    Hi, i'm new at this forum, and well, as i'm not too familiar with C#, i made a search on populating different controls using a row from a DataGridView, but i couldn't find anything that indeed showed how to do it.
    I'm sry for the inconvenience, but i need some help here.

    Greetings
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    The clearest set of examples I have seen are given by Brian Noyes in his book "Data Binding With Windows Forms 2.0" published by Addison-Wesley. Here is a sample chapter, but unfortunately it is not the exact chapter you need. It will give you an idea what to look for though:
    Presenting Data with the DataGridView

    If you want to bind several controls to a DataGridView row, you may need to clarify your objective a bit.

    Is there only one row of data you want to bind to? In that case, you would set up some type of BindingSource object that wraps the Row, and use the Control.DataBin dings.Add() method to bind a control property (such as 'Text') to a property (column value) of the row.

    However, if you want a set of controls to display different values for several different rows, then both the DataGridView and the Controls need to share a common BindingSource object which wraps a collection of class instances, where each instance holds one complete set of data you want to display. (Possibly (all depends on your UI) a BindingNavigato r would ease navigation).

    Both the controls and the DataGrid would bind to individual properties of each object instance, and the BindingSource wraps a collection of these instances.

    So for example, if you have a Customer class with Name, Id and StoreLocation, you would have a collection of Customers (say a List<Customer>, a BindingSource whose DataSource is the List<Customers> , and both the DataGridView and the set of controls will individually bind to that BindingSource.

    Note that this allows you to remove or change the DataGridView or individual controls without affecting the underlying binding. In other words, do not try to tightly couple your control binding to the DataGridView. Use a BindingSource to implement a loosely coupled architecture.

    Comment

    Working...