Solved

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

Posted on 2008-06-23
5
290 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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