Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / January 2008

Tip: Looking for answers? Try searching our database.

DataGridView, updating values and Resetbindings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Schwann - 28 Jan 2008 17:14 GMT
Hi group,

First: .NET V2.0 (cannot upgrade)

I am trying to do my best with a DataGridView but I have several
issues. To illustrate what I have so far (more or less), some example
code first (questions are below the code):

There is a form which contains nothing but a DataGridView, the ctor
looks like:

      public Form1() {
           InitializeComponent();

           resetBindingsDelegate = new
ResetBindingsDelegate(resetBindings);

           for (int i = 0; i < 30; i++) {
               this.bindingSource1.Add(new Record());
           }

           t = new Thread(new ThreadStart(run));
           t.Start();
       }

The DataGridView will have 30 rows containing a Record. The Record
class is:

  public class Record {
       private long b;

       public Record() {
           b = 0;
       }

       public long B {
           get { return b; }
           set { b = value; }
       }
   }

Nothing special here... The thread started in the Form1 ctor looks
like:

       private void run() {
               do {
                   long newValue = System.DateTime.Now.Ticks;

                   IList<Record> records = this.bindingSource1.List
as IList<Record>;

                   foreach (Record r in records) {
                       r.B = newValue;
                   }

                   Invoke(resetBindingsDelegate, new Object[]
{false});

                   Thread.Sleep(50);
               } while (true);
       }

       private void resetBindings(bool metadatachanged)
       {
           this.bindingSource1.ResetBindings(metadatachanged);
       }

It does nothing but setting the current tickstime in the records's
property B.

Here are my questions:

1) Without the resetBindings() call, the new values do not show up in
the DataGridView. My feeling is that this should work (but it
doesn't). What am I doing wrong? What is the right way to update data?
To be honest: The values are there and if I select a cell, the current
value is shown. But it doesn't work automatically.
2) In case the resetBindings() call is correct, I have the problem
that each call automatically selects a cell, the old selection is
somtimes lost. The big problem is that the DataGridView scrolls
automatically to the selected cell. Thus, I cannot scroll down because
ResetBindings selects automatically the first cell (well, I can scroll
down but it immediately jumps to the first position - where the
selection is). Can I somehow make ResetBindings stop to manipulate the
cell selection?
3) I also found a method ResetItem(index). This solves all the issues
above but it slows down the application dramatically. For each cell
change, I have to call it and this call has to go via a delegate.
4) Is there any useful tutorial available explaining how DataGridView/
DataBinding work and how make a fast and useful table out of it? The
Microsoft documentation is not very helpfull...

Thanks for all your ideas and suggestions!

Regards,
Paul
Jack Jackson - 28 Jan 2008 19:40 GMT
Your record class needs to implement INotifyPropertyChanged, and each
Set method, when the value changes, needs to call
OnPropertyChanged("name-of-property").  That pushes property changes
back to the data consumer.

To do this I created a class that has all of the necessary plumbing,
then derive my row classes from it.  Not sure I got all of the C#
right, but you should get the idea:

Public Class BindingListItem
   Implements INotifyPropertyChanged

   Public Event PropertyChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.PropertyChangedEventArgs) Implements
System.ComponentModel.INotifyPropertyChanged.PropertyChanged

   Protected Sub OnPropertyChanged(ByVal e As
System.ComponentModel.PropertyChangedEventArgs)
       RaiseEvent PropertyChanged(Me, e)
   End Sub

   Protected Sub OnPropertyChanged(ByVal propertyName As String)
       OnPropertyChanged(New PropertyChangedEventArgs(propertyName))
   End Sub
End Class

  public class Record : BindingListItem {
       private long b;

       public Record() {
           b = 0;
       }

       public long B {
           get { return b; }
           set {
      if ( b != value ) {
                b = value;
                OnPropertyChanged("B");
           }
       }
   }

I normally do this sort of thing with some kind of list, usually a
generic List(Of T), with the list being the BindingSource's DataSource
and the BindingSource the grid's DataSource.  That lets me access the
list items more directly through the list without having to use casts.

>Hi group,
>
[quoted text clipped - 92 lines]
>Regards,
>Paul

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.