none
N-Tier, Table, DataGridview, TextBoxs, ObjClass, How to bind them, ? RRS feed

  • Question

  • HI

    I'm new so I have many dubts about the best way to bind object in my application

    I Have a simple N-Tier Application with UI, BLL a DAL Layers

    Then I have a DB Table, a DataGridview (DGV) , several textboxs(Textbox1, Texbox2 etc)  in UI Form,  and 1 objclass (OBJ) that has only 3 Properties ( NumProg,Sigla,Descrizione)

    The DAL read DB and pass DataTable to BLL - BLL Pass DataTable to UI - so I have UITable1 in UI

    The following is the UI form

    1) It is important to know that I don't want the user add  o modify or delete Item directly in the DGV 

    He can do that using the small box on the right oof DGV with textbox and Command in the bottum Menustrip to ADD, MODIFY, DELETE rows in DB Table

    2) I don't want to use TableAdapter but only Dataset and DataTable

    3) I have an BindingSource (BS1) with BS1.dataSource = UITAble   and   DGV.DataSOurce = BS1

    How to bind other things ?

    Solution 1) Bind TextBox ti DGV Colums then  OBJ to Textbox then pass OBJ to BLL to 

    Solution 2) Bind OBJ to DGV colums then TextBox to OBJ then pass OBJ to BLL

    Solution 3 ) Bind TextBox to UITAble1 and ???????? how update DGV ?

    I'm very confused ?

    Can you help Me ?

    Thank you

    Claudio

    Thursday, January 11, 2018 9:44 AM

Answers

  • Hello,

    High level, use set the the table to a BindingSource where the BindingSource becomes the data source of the DataGridView. Then for controls such as TextBox, ComboBox etc use SomeTextBox. Binding via a BindingSource is extremely powerful.

    The DataGridView is bound via DataGridView.DataSource = bs (a BindingSource).

    Example

    TextBox1.DataBindings.Add("Text",bs,"SomeFieldName")

    Depending on what actions you want you can cast via BindingSource.Current down to say a DataRow

    CType(bs.Current,DataRowView).Row

    Get at a field

    CType(bs.Current,DataRowView).Row.Field(Of String)("FirstName")

    Or 

    CType(bs.Current,DataRowView).Row.SetField(Of String)("FirstName","Karen")

    Perhaps to cancel an operation

    bs.RejectChanges

    Bottom line is you need to learn what each component and control is capable of meaning what is available say for a BindingSource as in methods and properties. This goes to a DataTable to. Look at the events for say a DataTable which many times is overlooked e.g. see this code sample. Another on data binding. Here is a primer on data binding working with ms-access but the same applies for sql-server or oracle etc.. 

    I could go on but that should be enough to start with.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Thursday, January 11, 2018 3:22 PM
    Thursday, January 11, 2018 11:22 AM
    Moderator

All replies

  • Hello,

    High level, use set the the table to a BindingSource where the BindingSource becomes the data source of the DataGridView. Then for controls such as TextBox, ComboBox etc use SomeTextBox. Binding via a BindingSource is extremely powerful.

    The DataGridView is bound via DataGridView.DataSource = bs (a BindingSource).

    Example

    TextBox1.DataBindings.Add("Text",bs,"SomeFieldName")

    Depending on what actions you want you can cast via BindingSource.Current down to say a DataRow

    CType(bs.Current,DataRowView).Row

    Get at a field

    CType(bs.Current,DataRowView).Row.Field(Of String)("FirstName")

    Or 

    CType(bs.Current,DataRowView).Row.SetField(Of String)("FirstName","Karen")

    Perhaps to cancel an operation

    bs.RejectChanges

    Bottom line is you need to learn what each component and control is capable of meaning what is available say for a BindingSource as in methods and properties. This goes to a DataTable to. Look at the events for say a DataTable which many times is overlooked e.g. see this code sample. Another on data binding. Here is a primer on data binding working with ms-access but the same applies for sql-server or oracle etc.. 

    I could go on but that should be enough to start with.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Thursday, January 11, 2018 3:22 PM
    Thursday, January 11, 2018 11:22 AM
    Moderator
  • Hi Karen

    I have to study today, but what I understood is that the TextBoxes must bind to the same Table as the DGV is binded  to

    So I do not need the ObJClass 

    Right ?

    Thursday, January 11, 2018 11:47 AM
  • I know it is senseless to write something which can help you. But for others finding this thread. 

    Binding means working with references. A reference is nothing more than a pointer which as it is done well, goes as far as the DAL. 


    Success Cor

    Thursday, January 11, 2018 12:05 PM
  • what do you mean Cor ?

    I use the DAL to read data from Database and get Table

    Thursday, January 11, 2018 12:24 PM
  • Hi Karen

    I have to study today, but what I understood is that the TextBoxes must bind to the same Table as the DGV is binded  to

    So I do not need the ObJClass 

    Right ?

    Why would that be a restriction?

    You can bind to whatever you want, a DataTable in one BindingSource or even BindingSource to BindingSource that has a foreign key between the two e.g. master-detail. Example is in my code sample for doing master/detail(master)/details meaning in plain English example.

    Master is customers

    Details is orders

    Master-detail is order details

    Or in this code sample Product/ProductColor/ProductSize

    Then

    It doesn't matter how one gets to the data, n-tier or not, the same applies once at the form level. Here is another example, this one is done a little differently but still the same concept, still three BindingSource components. This one shows master-details, it has a good deal of things within to look at. While this one is in C#, still the same pattern.


    Note I have not done a n-tier code sample but we do a repository pattern when creating web applications where a repository pattern is actually goes much deeper e.g. using partial classes for uniformity, DTO (Data Transfer Objects) and dependency injection which really is overkill in desktop applications. Back on topic, bottom line is what I provided above is a pattern that can be fit into your model but first you need to get to a point (and this really needs to happen by studying and attempting to work through classes, methods, properties and events at a simple level).  I think it's great you are going this route yet when you say "I'm confused" means first studying rather than jumping head first (and I mean no disrespect) into n-tier pattern.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 11, 2018 12:28 PM
    Moderator
  • Hi Karen

    The problem in not N-Tier that is a simple concept to have code separaed in layers

    The problems is that there are many ways to do the same things and I have to choose only one and follow that one for all the application.

    Unfortnumatly there is not a real tutorial book that explains binding from basic to advanced but just hundreds of samples in the web and they confuse me.

    Your samples help me a lot and every one let me understand what to study, but it is very difficult at the end to merge all the concepts.

    But i don't know other ways to learn.

    Thursday, January 11, 2018 1:26 PM
  • what do you mean Cor ?

    I use the DAL to read data from Database and get Table

    That is not the problem, you want to solve something in "Mine way" (yours) and then ask help how to do that. 

    You tell for instance that you want to use a dataset for one table with one row of data which is more simple done in much easier ways. 

    Nobody can help you doing things in "your way" which is different than normal.


    Success Cor

    Thursday, January 11, 2018 1:37 PM
  • Hi Core, You must know that i will develop a business application with several tables with hundreds of records. I ask help for one table and one row for semplicity I'm intersted on the logic to use regatdless the number of tables and row. Bye
    Thursday, January 11, 2018 2:44 PM
  • The default, GUI-configured DataBinding in Windows Forms is meant for simplicity and speed of development.  It is not meant to conform to a particular design pattern.  That being the case, you should have many doubts about how to use it because it does not lend itself well to a structured application design such as an N-Tier database application.

    What this means is that you probably will not use any of the GUI-based or wizard-driven binding functionality in the designer.  Instead you will write code that performs UI binding between a data source and particular UI controls, when applicable (you may find it easier to just get/set property values since you are writing code anyway).  In this case the "data source" would be an instance of your Business Logic Layer.  You'll have the BLL provide collections of data to the UI as appropriate for your desired business logic functionality.  If a collection of items is readonly in the UI then provide the UI with only an IEnumerable collection of that object.  If you're not updating the values then there really is no "binding" necessary; just display the data and update the display if it changes in some other way.  The point is that your code will need to handle displaying data and updating data with user provided changes.  DataBinding will probably be of limited use to you.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, January 11, 2018 3:26 PM
    Moderator