Locations of visitors to this page
Onteora Software - January 2008

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Datagridview and Linq issue

I had some one ask me an interesting question about using linq with the datagridview

When I bind a datagridview to this query

Dim names() As String = {"hello11", "hello212", "hello123", "hello124", "hello2325", "hello336", "hello457"}
Dim query = From s In names _
            Order By s _
            Select s
Dim bs As New BindingSource
bs.DataSource = query
DataGridView1.DataSource = bs

Why do I get these results?

image

The answer the datagridview will show the properties of the class in the list bound to the datagridview.  In this case we are bound to a list of string and the only bindable property is its Length.  

If you change the query to this

Dim names() As String = {"hello11", "hello212", "hello123", "hello124", "hello2325", "hello336", "hello457"}
Dim query = From s In names _
            Order By s _
            Select New With {.Name = s}
Dim bs As New BindingSource
bs.DataSource = query
DataGridView1.DataSource = bs

You will get the expected results

image  

I should also mention that you can not bind a datagridview to a linq query directory.  You need to bind the query to a bindingsource and bind the datagridview to the bindingsource or bind the datagridview to the queries ToList method.



kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DataGridView | Linq | VB
Posted by Ken Tucker on Monday, January 28, 2008 6:35 AM
Permalink | Comments (1) | Post RSSRSS comment feed

John Papa coming to Space Coast .Net User Group

John Papa will be speaking about the Entity Framework at the Space Coast .Net User Group Feb 20,2008 meeting.

The ADO.NET Entity Framework, part of ADO.NET components of the .NET Framework, is an Object-Relational mapping technology from Microsoft. It is geared to solving the mismatch between the formats data is stored in a database and in which it is consumed in an object-oriented programming language or other front ends. ADO.NET Entity Framework will be released in the first half of 2008, separate from .NET Framework 3.5 and Visual Studio 2008.

John Papa, Senior Consultant for ASPSoft, Inc., a Microsoft MVP [C#], MCSD.NET, and an author of several XML, ADO and SQL books will be speaking. John has over 10 years experience in architecting and developing Microsoft technologies as a consultant and a trainer. He is the author of the Data Points column in MSDN Magazine and can often be found speaking at user groups, MSDN Web Casts, and industry conferences such as VSLive.John Papa, Senior Consultant for ASPSoft, Inc., a Microsoft MVP [C#], MCSD.NET, and an author of several XML, ADO and SQL books will be speaking.

Please register if you plan to attend this event.

https://www.codezone.com/UGEventView.CodezoneCom?EventID=4987

 

 Be sure and check out John's Article on the Entity Frame in the MSDN magazine

http://msdn.microsoft.com/msdnmag/issues/08/02/DataPoints/default.aspx

 

 



kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Events
Posted by Ken Tucker on Sunday, January 27, 2008 1:01 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Sync Services Part 2

In the second post of this series we will make it so changes we make to the local data will be sent back to the server.  Lets start by opening the project we created in the previous post.

 

So lets extend the SyncAgent Partial classes to make the sync 2 way. The class contains a partial method OnInitialized which you can add code to.   In this method we will make the Products table Sync Direction Bidirectional.

 

Partial Public Class NorthwindSyncAgent

    Private Sub OnInitialized()
        Me.Products.SyncDirection = SyncDirection.Bidirectional
    End Sub
End Class

 

Now what if there is a conflict? Let create a form to give the user the option of keeping the changes he/she made or to accept the changes on the server.  Add a form named frmConflict to the project.  On the form add 2 buttons (btnClient, and btnServer) and 2 datagridviews (dgvClient, and dgvServer).  Here is what my form looks like

 

image

 

Add the following code to the buttons

Private Sub btnClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClient.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
End Sub

Private Sub btnServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnServer.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
End Sub

 

Now we need to extend the ServerSyncProvider to raise an event when there is a conflict. If the user presses the Keep my changes button we force the changes to be written to the database otherwise we allow the change to be made

 

Partial Public Class NorthwindServerSyncProvider

    Private Sub OnInitialized()
        AddHandler ApplyChangeFailed, AddressOf ApplyChangesFailed
    End Sub

    Public Sub ApplyChangesFailed(ByVal sender As Object, ByVal e As ApplyChangeFailedEventArgs)
        Dim frm As New frmConflict
        frm.dgvClient.DataSource = e.Conflict.ClientChange
        frm.dgvServer.DataSource = e.Conflict.ServerChange
        frm.ShowDialog()
        If frm.DialogResult = DialogResult.OK Then
            e.Action = ApplyAction.RetryWithForceWrite
        Else
            e.Action = ApplyAction.Continue
        End If
    End Sub

End Class



kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Ken Tucker on Saturday, January 26, 2008 8:59 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Sync Services Part 1

In this post we will create a local cache of the Northwind database.  To start with lets create a new visual basic windows forms project in Visual Studio 2008.  From the project menu select add a new item and select a new local database cache and name it northwind.

 

image

 

In the server connect select a connection to the northwind database.

 

Press the add button and select the product table.  Press OK to close the dialog.  Go ahead and create a table adapter for the product table.  The drag the products table on to the form from the data source window.  Add a button to the binding navigator and set its text to Sync and change the display style to Text.

 

In the button you added to the toolbar add this code

 

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

       ' Update the database

        Me.ProductsBindingSource.EndEdit()         Me.TableAdapterManager.UpdateAll(Me.NorthwindDataSet)

        ' Call SyncAgent.Synchronize() to initiate the synchronization process.

        ' Synchronization only updates the local database, not your project’s data source.

        Dim syncAgent As ProductsSyncAgent = New ProductsSyncAgent()         Dim syncStats As Microsoft.Synchronization.Data.SyncStatistics = syncAgent.Synchronize()

       ' Reload the data source from the local database

        Me.ProductsTableAdapter.Fill(Me.NorthwindDataSet.Products)

End Sub

Run the app and Open up the Sql Server Management Studio Express.  Make some changes in the Northwind database's Product table and Press the sync button.  

 

Notice the changes you made to the Products table show up in the datagridview.  The changes are also saved in the local sqlce database.



kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Ado.Net | Sql | VB
Posted by Ken Tucker on Saturday, January 26, 2008 4:21 PM
Permalink | Comments (1) | Post RSSRSS comment feed