MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DataGridView, updating values and Resetbindings

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> Windows Forms Controls
Author Message
Paul Schwann



Joined: 26 Dec 2007
Posts: 1

PostPosted: Mon Jan 28, 2008 2:14 pm    Post subject: DataGridView, updating values and Resetbindings Reply with quote

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 records = this.bindingSource1.List
as IList;

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

Archived from group: microsoft>public>dotnet>framework>windowsforms>controls
Back to top
View user's profile Send private message
Jack Jackson



Joined: 01 Jan 2008
Posts: 7

PostPosted: Mon Jan 28, 2008 4:40 pm    Post subject: Re: DataGridView, updating values and Resetbindings Reply with quote

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.

On Mon, 28 Jan 2008 09:14:47 -0800 (PST), Paul Schwann
wrote:

>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 records = this.bindingSource1.List
>as IList;
>
> 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
>

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Set Default Values in DataGridView - Repost This is a re-post, I have not found anything that works - yet. I would like to set the default value of a dataviewgrid column (I have combobox columns and checkbox columns) to the previous value in the row above, I don't want all column values set to a de

remove row in datagridview Hello, How to remove a Row in a DataGridView? My DataGridView is readonly and there is no binding (Cells are build I tried the following rowIndex ) and row ) but an exception is a

Clear DataGridView (VB.NET 2.0) Hi to all, I'd like to know how I can clear the DataGridView Control. Thanks for your support Fulvio -- Fulvio Giaccari FreeAspx.it ASP & .NET Community

Datagridview, reorder columns Hi, I use bound datagridview control of VS 2005. I want to remember the order of columns, which users make at runtime, the next time I will show the columns in this ordering. For Example: My Grid had 3 Columns: ID, Name, Size. In runtime users reorder the

DataGridView RowValidating IndexOutOfRangeException Hi all, I am having a problem with inserting rows in to my datagridview control. The datagridview is bound to a bindingsource with a filter set. If i try and add a row to the datagridview while the filter is set i get an specifyin
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> Windows Forms Controls All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group