Solved

DatagridView Bound to SourceBindingSource - How to clear the grid and reload it?

Posted on 2008-06-23
5
287 Views
Last Modified: 2008-06-23
I have a windows desktop application (VB.NET 2008) and a DataViewGrid.  Using the Add Project Data Source to the DataGridView via it's DataGridViewTasks,  I have build a BindingSource which is associated with a class within this project.  

I add rows to the grid easily with commands like the following:
 Me.MyGridSourceBindingSource.Add(MyGridSource.NewMyGridSource(sFileDate, sFileFolder, sFile, CType(sFileSize, Long), bDoesFileExist)).  This method returns an object of the class that I based the BindingSource on.  All well and good.

The questions are as follows:
1) How to clear the grid for subsequent inserts?
2) How to associate a grid with a bindingsource,  modifiy the underlying class and have the changes appear in the grid?  At the moment,  I seem to have to delete the grid and then attach it to the bindingsource to accomplish that.


0
Comment
Question by:Howard Bash
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:graye
ID: 21846521
The typical way you'd expect a DataGridView to operate with an class-based "object" datasource would be....   You'd create a BindingSource (just one time) that uses a collection of objects (or something similar to a collection like a strongly-typed list, etc).   Then you'd add, remove, update the instances of the classes in the collection/list.   The DataGridView would automagically see the changes and disaply the results.

So you'd perform all of the operations on the collection/list directly... not by manipulatin the BindingSource.

So, in this scenario, to clear the datagrid, you'd just delete the items in the collection/list
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 21846628
I use the following to add rows to the BindingSource.  These rows then appear in the grid.
Me.MoveSheetSourceBindingSource.Add(MoveSheetSource.NewMoveSheetSource(sFileDate, sFileFolder, sFile, CType(sFileSize, Long), bDoesFileExist))

The MoveSheetSourceBindingSource does not have a clear or remove method.  Can you point me to the object to clear?

Thanks,
Howard

0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 21846888
Well, I was suggesting a different technique... where you'd create and modify a collection of objects instead of manipulating the BindingSource.

Here is a (very) trivial example of the technique I was suggesting.

If that's not what you're looking for... then tell us more about your application and the NewMoveSheetSource() method
Public Class Form1
 

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

        ' create a strongly-type list of Class1 objects

        Dim col As New List(Of Class1)
 

        ' add a few items

        col.Add(New Class1("John Doe", "123 Any Street"))

        col.Add(New Class1("Jane Doe", "666 Evil Way"))
 

        '  bind the list to the DataGridView

        DataGridView1.DataSource = col

    End Sub

End Class
 

Public Class Class1
 

    Private pName As String

    Private pAddress As String
 

    Sub New(ByVal Name As String, ByVal Address As String)

        pName = Name

        pAddress = Address

    End Sub
 

    Public Property Name() As String

        Get

            Return pName

        End Get

        Set(ByVal value As String)

            pName = value

        End Set

    End Property
 

    Public Property Address() As String

        Get

            Return pAddress

        End Get

        Set(ByVal value As String)

            pAddress = value

        End Set

    End Property

End Class

Open in new window

0
 
LVL 1

Author Comment

by:Howard Bash
ID: 21848134
I sent you the class and the button click event code.  I would like to clear the grid/source at the beginning of each click.


Imports System.ComponentModel
 

Public Class MoveSheetSource

    Implements INotifyPropertyChanged
 

    Public Event PropertyChanged(ByVal sender As Object, _

        ByVal e As PropertyChangedEventArgs) _

        Implements INotifyPropertyChanged.PropertyChanged
 

    Private m_FileFolder As String = String.Empty

    Private m_FileName As String = String.Empty

    Private m_FileExists As Boolean = False

    Private m_FileDate As String = String.Empty

    Private m_FileSize As Long = 0
 

    Private Sub New()

        ' require use of factory method

    End Sub
 

    Public Shared Function NewMoveSheetSource( _

      ByVal FileDate As String, _

      ByVal FileFolder As String, ByVal FileName As String, _

      ByVal FileSize As Long, ByVal FileExists As Boolean) As MoveSheetSource
 

        Dim msheet As New MoveSheetSource

        With msheet

            .FileDate = FileDate

            .FileFolder = FileFolder

            .FileName = FileName

            .FileSize = FileSize

            .FileExists = FileExists

        End With
 

        Return msheet
 

    End Function
 

    Public Property FileFolder() As String

        Get

            Return m_FileFolder

        End Get

        Set(ByVal value As String)

            m_FileFolder = value

            RaiseEvent PropertyChanged( _

              Me, New PropertyChangedEventArgs("FileFolder"))

        End Set

    End Property
 

    Public Property FileDate() As String

        Get

            Return m_FileDate

        End Get

        Set(ByVal value As String)

            m_FileDate = value

        End Set

    End Property
 

    Public Property FileName() As String

        Get

            Return m_FileName

        End Get

        Set(ByVal value As String)

            m_FileName = value

            RaiseEvent PropertyChanged( _

              Me, New PropertyChangedEventArgs("FileName"))

        End Set

    End Property
 

    Public Property FileExists() As Boolean

        Get

            Return m_FileExists

        End Get

        Set(ByVal value As Boolean)

            m_FileExists = value

            RaiseEvent PropertyChanged( _

              Me, New PropertyChangedEventArgs("FileExists"))

        End Set

    End Property
 

    Public Property FileSize() As Long

        Get

            Return m_FileSize

        End Get

        Set(ByVal value As Long)

            m_FileSize = value

            RaiseEvent PropertyChanged( _

              Me, New PropertyChangedEventArgs("FileSize"))

        End Set

    End Property
 

End Class
 

'''

'Button Click

'''

    Private Function LoadMoveSheetDestinationGrid() As Boolean

        Dim nCnt As Integer = 0, nlp As Integer = 0

        Dim sFile As String = String.Empty

        Dim sFileSize As String = String.Empty

        Dim sFileFolder As String = String.Empty

        Dim sFileDate As String = String.Empty

        Dim bDoesFileExist As Boolean = False
 

        Dim fileDetail As IO.FileInfo
 

        nCnt = m_FileArray.Count

        For nlp = 0 To nCnt - 1

            sFile = m_FileArray(nlp)

            If System.IO.File.Exists(sFile) Then

                bDoesFileExist = True

                fileDetail = My.Computer.FileSystem.GetFileInfo(sFile)

                sFileFolder = GetFolderName(sFile)

                sFile = GetFileName(sFile)

                sFileSize = fileDetail.Length.ToString

                sFileDate = fileDetail.LastWriteTime.ToString

            Else

                bDoesFileExist = False

                sFileFolder = GetFolderName(sFile)

                sFile = GetFileName(sFile)

                sFileSize = "0"

                sFileDate = ""

            End If
 

            Me.MoveSheetSourceBindingSource.Add(MoveSheetSource.NewMoveSheetSource(sFileDate, sFileFolder, sFile, CType(sFileSize, Long), bDoesFileExist))
 

        Next
 

    End Function
 

'''

Open in new window

0
 
LVL 1

Author Comment

by:Howard Bash
ID: 21848673
Ok.  I found the problem.  I was adding to my source data array per button click.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Runtime Error 2 28
Advice on Xojo as a development tool over VB. 4 39
Variable Event ? 3 22
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now