Solved

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

Posted on 2008-06-23
5
284 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:hbash
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:graye
Comment Utility
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:hbash
Comment Utility
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
Comment Utility
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:hbash
Comment Utility
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:hbash
Comment Utility
Ok.  I found the problem.  I was adding to my source data array per button click.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

5 Experts available now in Live!

Get 1:1 Help Now