Christian Burgener

Hi,

I try to build an application which has an overview form with a DataGridView and a detail form with the appropriate text fields.

Who can tell me now how to bind the DataSet, BindingSource and the BindingNavigator of the detail form to the overview form with the datagrid

BR / Chris



Re: Windows Forms Data Controls and Databinding Databinding with overview and detail form


Re: Windows Forms Data Controls and Databinding Databinding with overview and detail form

Gavin Jin - MSFT

Hi٬please download here ,there is a detail sample in the \Data Binding\General Samples\NYFoods folder.

Hope it helps.

The main code is here:

Public Class OrderForm

Private Flags As New Dictionary(Of String, Bitmap)

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
FillFlags()
End Sub

Private Sub OrderForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Fill DataSet
Me.NorthwindDataSet1.ReadXml("Customers.xml")
End Sub

#Region " Helper Functions "

Private Sub FillFlags()
' Fill Flags Hashtable (we don't want to be slow)
Dim image As System.Drawing.Bitmap
Dim parts() As String
Dim mytype As System.Type = Me.GetType()

For Each res As String In mytype.Assembly.GetManifestResourceNames()
' Find the image resources (.gif) and put them in the hashtable
If res.ToLower.EndsWith(".gif") Then
' Found an Image - extract the name
parts = res.Split(".")
' Add to hashtable
If (parts.Length > 1) Then
Dim key As String = parts(parts.Length - 2)
image = New Bitmap(mytype.Assembly.GetManifestResourceStream(res))
Flags(key) = image
End If
End If
Next
End Sub

#End Region

Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If ((e.ColumnIndex = 0) And (e.RowIndex >= 0)) Then

Dim g As Graphics = e.Graphics
Dim font As Font = e.CellStyle.Font

Dim selected As Boolean = ((e.State And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected)
Dim fcolor As Color
Dim bcolor As Color

If (selected) Then
fcolor = e.CellStyle.SelectionForeColor
bcolor = e.CellStyle.SelectionBackColor
Else
fcolor = e.CellStyle.ForeColor
bcolor = e.CellStyle.BackColor
End If

Dim row As DataRowView = Me.CustomersBindingSource(e.RowIndex)

Dim companyName As String = row("CompanyName")
Dim contactName As String = row("ContactName")
Dim id As String = row("CustomerID")

Dim sz As Size = TextRenderer.MeasureText(e.Graphics, companyName, font)

Dim x As Integer = e.CellBounds.Left + e.CellStyle.Padding.Left + 4
Dim y As Integer = e.CellBounds.Top + e.CellStyle.Padding.Top + 4

Dim width As Integer = e.CellBounds.Width - (e.CellStyle.Padding.Left + e.CellStyle.Padding.Right)
Dim height As Integer = sz.Height + (e.CellStyle.Padding.Top + e.CellStyle.Padding.Bottom)

g.FillRectangle(New SolidBrush(bcolor), e.CellBounds)

' Paint default
e.Paint(e.ClipBounds, DataGridViewPaintParts.Border Or DataGridViewPaintParts.Focus)

' Draw first line
TextRenderer.DrawText(e.Graphics, id, font, New Rectangle(x, y, width, height), fcolor, TextFormatFlags.PreserveGraphicsClipping Or TextFormatFlags.EndEllipsis Or TextFormatFlags.HorizontalCenter)

y = y + height + 1
TextRenderer.DrawText(e.Graphics, contactName, font, New Rectangle(x, y, width, height), fcolor, TextFormatFlags.PreserveGraphicsClipping Or TextFormatFlags.EndEllipsis Or TextFormatFlags.HorizontalCenter)

y = y + height + 1
TextRenderer.DrawText(e.Graphics, companyName, font, New Rectangle(x, y, width, height), fcolor, TextFormatFlags.PreserveGraphicsClipping Or TextFormatFlags.EndEllipsis Or TextFormatFlags.HorizontalCenter)

e.Handled = True
End If
End Sub

Private Sub ShowProgress(ByVal start As Boolean)
Me.ToolStripProgressBar1.Visible = start
Me.ToolStripProgressBar1.Value = 0

If (start) Then
Me.ProgressTimer.Start()
Else
Me.ProgressTimer.Stop()
End If
End Sub

Private Sub GetOrdersForCustomer(ByVal id As String)
' Clear Orders Grid
Me.OrdersBindingSource.DataSource = Nothing

' Set cursor
Me.UseWaitCursor = True

' Start progress bar
ShowProgress(True)

' Load orders async
Me.OrdersWorker.RunWorkerAsync(id)
End Sub

Private Sub CustomersBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomersBindingSource.CurrentChanged
' Get current row
Dim row As DataRowView = Me.CustomersBindingSource.Current

If Not row Is Nothing Then
' Show the flag
Dim country As String = row("Country")
Dim id As String = row("CustomerID")

' Set Flag
If Flags.ContainsKey(country) Then
Me.FlagsPictureBox.Image = Flags(country)
End If

' Load Orders
GetOrdersForCustomer(id)
End If
End Sub

Dim m_rand As System.Random = New System.Random()

Private Function GetOrdersFromSimulatedWebService(ByVal id As String) As DataView
' Simulate web service call
Dim ds As New NorthwindDataSet
Dim view As DataView

' Load from file rather than web service
ds.ReadXml("Orders.xml")

' Set view
view = ds.Orders.DefaultView
view.RowFilter = "CustomerID = '" + id + "'"

' Simulate load
Threading.Thread.Sleep(m_rand.Next(1, 4) * 1000)

' Return view
Return view
End Function

Private Sub OrdersWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles OrdersWorker.DoWork
' Get orders from web service (simulated)
e.Result = GetOrdersFromSimulatedWebService(e.Argument)
End Sub

Private Sub OrdersWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles OrdersWorker.RunWorkerCompleted
' Bind to result
Me.OrdersBindingSource.DataSource = e.Result

' End progress
ShowProgress(False)

' Reset cursor
Me.UseWaitCursor = False
Me.OrdersDataGridView.Cursor = Cursors.Default
End Sub

Private Sub ProgressTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressTimer.Tick
Me.ToolStripProgressBar1.PerformStep()
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
End Class