Is using bound controls still a sin in .Net?

Sethi
Sethi used Ask the Experts™
on
In VB6 it was sin to use bound controls. In VB.Net I have not come across any concrete discussion on bound controls where they are untouchable. Any views on this?

For example, DataGrids is one of my most common control for data display. Now everywhere in books and documentation Datasets are being used to bind DataGrids with them. I am however, in old habit of using Unbound FlexGrids in VB6. So now I am not sure what is the best way to approach in VB.net.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
No, absolutely not!  In fact, in .Net, there are many more things to bind!  Not only can you bind controls to datasources, but you can bind control properties to things such as Settings.  Also, the types of things that you can bind controls to has been expanded, such as a DataSet, DataView, BindingSource, Collections, etc...
Top Expert 2007

Commented:
Here's an example of control biding:
If you want to remember where your form was positioned at last, so you could restore it to the same location:

Go to the properties of your form, then go to ApplicationSettings at the top, then to (PropertyBinding).
Find Location and click the elipsis (...)
    Create a new setting named "Location", and click OK. and close your ApplicationSettings Window.

Then, in your Form_Closing event, you can save the new properties:
        My.Settings.Location = Me.Location
        My.Settings.Save()

Next time the form is opened, the location will automatically be restored.

You can do this for any property for any control - even your datagridview.
Top Expert 2007

Commented:
Concerning DataGrids, DataGridViews, and many other controls, you can also programmatically load them, if you want, however I would strongly suggest binding them in .NET.

Here's another example, - this is creating a DataGridView and binding DataView to it (which you can use to Sort and Filter the data in the DataTable with).

        Dim rate As Single = 0.0862
        Dim table As DataTable = New DataTable("MyTable")

        ' Create the first column.
        Dim priceColumn As DataColumn = New DataColumn
        With priceColumn
            .DataType = System.Type.GetType("System.Decimal")
            .ColumnName = "price"
            .DefaultValue = 50
        End With

        ' Create the second, calculated, column.
        Dim taxColumn As DataColumn = New DataColumn
        With taxColumn
            .DataType = System.Type.GetType("System.Decimal")
            .ColumnName = "tax"
            .Expression = "price * 0.0862"
        End With

        ' Create third column
        Dim totalColumn As DataColumn = New DataColumn
        With totalColumn
            .DataType = System.Type.GetType("System.Decimal")
            .ColumnName = "total"
            .Expression = "price + tax"
        End With

        ' Add columns to DataTable
        With table.Columns
            .Add(priceColumn)
            .Add(taxColumn)
            .Add(totalColumn)
        End With

        Dim row As DataRow = table.NewRow
        table.Rows.Add(row)
        Dim view As New DataView
        view.Table = table
        DataGrid1.DataSource = view
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Top Expert 2007

Commented:
Here's 1 more example of manually binding data to TextBoxes:


        Dim rate As Single = 0.0862
        Dim table As DataTable = New DataTable("MyTable")

        ' Create the first column.
        Dim priceColumn As DataColumn = New DataColumn("Price")
        With priceColumn
            .DataType = System.Type.GetType("System.Decimal")
            .ColumnName = "price"
            .DefaultValue = 50
        End With

        ' Create the second, calculated, column.
        Dim taxColumn As DataColumn = New DataColumn("Tax")
        With taxColumn
            .DataType = System.Type.GetType("System.Decimal")
            .ColumnName = "tax"
            .Expression = "price * 0.0862"
        End With

        ' Create third column
        Dim totalColumn As DataColumn = New DataColumn("Total")
        With totalColumn
            .DataType = System.Type.GetType("System.Decimal")
            .ColumnName = "total"
            .Expression = "price + tax"
        End With

        ' Add columns to DataTable
        With table.Columns
            .Add(priceColumn)
            .Add(taxColumn)
            .Add(totalColumn)
        End With

        Dim row As DataRow = table.NewRow
        table.Rows.Add(row)
        Dim view As New DataView
        view.Table = table


        'Manual binding to TextBoxes
        Me.TextBox1.DataBindings.Add(New Binding("Text", view, "Price"))
        Me.TextBox2.DataBindings.Add(New Binding("Text", view, "Tax"))
        Me.TextBox3.DataBindings.Add(New Binding("Text", view, "Total"))


'Drop a BindingNavigator on your form to use for navigating your data, and it will manage your data for you!

Author

Commented:
Wow. This is some news. So that means there is no tradeoff with performance when we use bound controls in .Net?
Top Expert 2007

Commented:
No.  In fact, the performance is better if you do it that way, rather than manually loading it.

Author

Commented:
I am not at all doubting your advise on this - but just inquisitive about it - are there any links where I can read more about this. I sure will take your advise and use bound controls now - but further reading is something I would really like to have.
Top Expert 2007

Commented:
Sure, let me look around.
Top Expert 2007

Commented:
Here's a pretty good article - there's 4 pages, so go to the bottom for the next page link.

http://www.code-magazine.com/Article.aspx?quickid=0507051

Author

Commented:
Thank you so much. I will have a look at it now.
Top Expert 2007

Commented:
Here's a pretty good article about the .NET 2.0 DataGridView.  It discusses both DataBinding and programmatic constrution of the grid:

http://www.codeproject.com/books/PresentDataDataGridView.asp

Author

Commented:
VBRocks really Rocks :-))
Top Expert 2007
Commented:
One final thing.  Here is an actual example for you.  It does a little benchmark testing.  You will find that using DataBinding to bind to a DataGridView is TWICE as fast as manually loading your grid:

Need items:
    Put a DataGridView on your form and name it:  "DataGridView1"
    Put a Button on your form and name it:  "Button1"
    Put a Button on your form and name it:  "Button2"

First, double-click your form and add the following code to the Form_Load event:
        AddHandler Button1.Click, AddressOf Button1_Click
        AddHandler Button2.Click, AddressOf Button2_Click

Next, paste all this code into your Form Class:

    Private ds As New DataSet

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sw As New Stopwatch()
        sw.Start()

        LoadData()

        Me.DataGridView1.DataMember = "MyTable"
        Me.DataGridView1.DataSource = ds

        sw.Stop()

        MsgBox(sw.Elapsed.Milliseconds.ToString())

    End Sub

    Private Sub LoadData()
        Dim dt As New DataTable("MyTable")

        'Add to dataset
        ds.Tables.Add(dt)

        Dim col1 As New DataColumn("ID")
        col1.AutoIncrement = True

        Dim col2 As New DataColumn("MyText")

        dt.Columns.AddRange(New DataColumn() {col1, col2})

        Dim row As DataRow
        For i As Integer = 1 To 100
            row = dt.NewRow()
            row.Item("MyText") = "This is my test text for row " & i - 1

            dt.Rows.Add(row)

        Next

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim sw As New Stopwatch
        sw.Start()

        Me.DataGridView1.Columns.Add("ID", "ID")
        Me.DataGridView1.Columns.Add("MyText", "MyText")


        For i As Integer = 1 To 100
            Me.DataGridView1.Rows.Add(i, "This is my test text for row " & i - 1)
        Next

        sw.Stop()

        MsgBox(sw.Elapsed.Milliseconds.ToString())


    End Sub


There you go!

Author

Commented:
:-) I would have loved to give you 1000 points for this effort but RAC ruls doesn't allow it. Just wanted to tell you that I appreciate this selfless effort that you have put into this issue. Thank you so much.
Top Expert 2007

Commented:
You're welcome!  I'm glad I could help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial