Derived DataGrid not displeyed correctly

G

Guest

Hi!

I derived a new class from a DataGrid control and added some functionality
to it. Among other things a method that autosizes the columns displayed.

When I change the data source and data member of the datagrid after having
updated some data in it, it sometimes does not display correctly. What
happens is that some cells from the old view is visible in the new view. I do
not know what is the problem, but my guess is that it has something to do
with the repainting of the derived datagrid.

Do I need to do something in the derived data grid to get the painting to
work correctly?
 
G

Guest

Hi ScubaD,

ScubaD said:
Hi!

I derived a new class from a DataGrid control and added some functionality
to it. Among other things a method that autosizes the columns displayed.

When I change the data source and data member of the datagrid after having
updated some data in it, it sometimes does not display correctly. What
happens is that some cells from the old view is visible in the new view. I do
not know what is the problem, but my guess is that it has something to do
with the repainting of the derived datagrid.

Do I need to do something in the derived data grid to get the painting to
work correctly?

Before you change the data source, call "SuspendLayout" on the DataGrid and
call ResumeLayout after making the changes. I believe that may solve your
issue.

If that doesn't work try the following:

BindingManagerBase bm = yourDataGrid.BindingContext [
yourDataGrid.DataSource,
yourDataGrid.DataMember
];
bm.SuspendBinding( );

// <-- modify the data source here

bm.ResumeBinding( );

Hope this helps,
 
G

Guest

Hi TT!

I have tried using SuspendLayou as you sugested. In my derived class I have
new DataSource and DataMember properties (look at the pasted code).

Unfortunatelly this does not help. Am I doing something wrong?

I told you earlier that the repainting doesn't seem to work when I edited
the cells in the first view. This is actually not the case. From what I can
see the problem occurs when I have selected a cell so that is is marked (just
click in a cell in a datagrid and the text is highlited). When I am in this
state and changes the data source (or datamember since I just shift between
tables in the dataset), then that specific row with the selected cell is not
repainted correctly in the new view.

----------------------------------------------------------
// DataSource and DataMember property

public new object DataSource
{
get { return base.DataSource; }
set
{
this.SuspendLayout();

if (this.AutoSave) SaveChanges();

if (value.GetType().GetInterface("Offertering.Data.IDataList") != null)
{
base.DataSource = ((Offertering.Data.IDataList)value).DataSource;
((DataSet)this.DataSource).AcceptChanges();
}
else if (value.GetType() == typeof(DataSet))
{
base.DataSource = value;
((DataSet)this.DataSource).AcceptChanges();
}

// New property
AllowNewRow = AllowNewRow;

this.ResumeLayout(false);
}
}

public new string DataMember
{
get { return base.DataMember; }
set
{
BindingManagerBase bm = null;

this.SuspendLayout();
if (this.AutoSave) SaveChanges();

base.DataMember = value;

// If DataSource is a DataSet, then implement AllowRow for the datamember
if (this.DataSource != null && this.DataSource.GetType() ==
typeof(DataSet))
{
// Implement the AllowNewRow setting for the new DataMember object
AllowNewRow = AllowNewRow;
}
}
}

--
/ ScubaD



TT (Tom Tempelaere) said:
Hi ScubaD,

ScubaD said:
Hi!

I derived a new class from a DataGrid control and added some functionality
to it. Among other things a method that autosizes the columns displayed.

When I change the data source and data member of the datagrid after having
updated some data in it, it sometimes does not display correctly. What
happens is that some cells from the old view is visible in the new view. I do
not know what is the problem, but my guess is that it has something to do
with the repainting of the derived datagrid.

Do I need to do something in the derived data grid to get the painting to
work correctly?

Before you change the data source, call "SuspendLayout" on the DataGrid and
call ResumeLayout after making the changes. I believe that may solve your
issue.

If that doesn't work try the following:

BindingManagerBase bm = yourDataGrid.BindingContext [
yourDataGrid.DataSource,
yourDataGrid.DataMember
];
bm.SuspendBinding( );

// <-- modify the data source here

bm.ResumeBinding( );

Hope this helps,
 
B

Bart Mermuys

hi,

// ScubaD said:
Hi TT!

I have tried using SuspendLayou as you sugested. In my derived class I
have
new DataSource and DataMember properties (look at the pasted code).

Unfortunatelly this does not help. Am I doing something wrong?

I told you earlier that the repainting doesn't seem to work when I edited
the cells in the first view. This is actually not the case. From what I
can
see the problem occurs when I have selected a cell so that is is marked
(just
click in a cell in a datagrid and the text is highlited). When I am in
this
state and changes the data source (or datamember since I just shift
between
tables in the dataset), then that specific row with the selected cell is
not
repainted correctly in the new view.

Something that has helped in the past for a similar problem, is calling
Select() just before changing the DataSource, not sure if this wil help your
problem :

dataGrid1.Select();
dataGrid1.DataSource = ... ;

HTH,
Greetings
----------------------------------------------------------
// DataSource and DataMember property

public new object DataSource
{
get { return base.DataSource; }
set
{
this.SuspendLayout();

if (this.AutoSave) SaveChanges();

if (value.GetType().GetInterface("Offertering.Data.IDataList") != null)
{
base.DataSource = ((Offertering.Data.IDataList)value).DataSource;
((DataSet)this.DataSource).AcceptChanges();
}
else if (value.GetType() == typeof(DataSet))
{
base.DataSource = value;
((DataSet)this.DataSource).AcceptChanges();
}

// New property
AllowNewRow = AllowNewRow;

this.ResumeLayout(false);
}
}

public new string DataMember
{
get { return base.DataMember; }
set
{
BindingManagerBase bm = null;

this.SuspendLayout();
if (this.AutoSave) SaveChanges();

base.DataMember = value;

// If DataSource is a DataSet, then implement AllowRow for the
datamember
if (this.DataSource != null && this.DataSource.GetType() ==
typeof(DataSet))
{
// Implement the AllowNewRow setting for the new DataMember object
AllowNewRow = AllowNewRow;
}
}
}

--
/ ScubaD



TT (Tom Tempelaere) said:
Hi ScubaD,

ScubaD said:
Hi!

I derived a new class from a DataGrid control and added some
functionality
to it. Among other things a method that autosizes the columns
displayed.

When I change the data source and data member of the datagrid after
having
updated some data in it, it sometimes does not display correctly. What
happens is that some cells from the old view is visible in the new
view. I do
not know what is the problem, but my guess is that it has something to
do
with the repainting of the derived datagrid.

Do I need to do something in the derived data grid to get the painting
to
work correctly?

Before you change the data source, call "SuspendLayout" on the DataGrid
and
call ResumeLayout after making the changes. I believe that may solve your
issue.

If that doesn't work try the following:

BindingManagerBase bm = yourDataGrid.BindingContext [
yourDataGrid.DataSource,
yourDataGrid.DataMember
];
bm.SuspendBinding( );

// <-- modify the data source here

bm.ResumeBinding( );

Hope this helps,
 
G

Guest

I tried calling Select() and that didn't help either.

--
/ ScubaD


Bart Mermuys said:
hi,

// ScubaD said:
Hi TT!

I have tried using SuspendLayou as you sugested. In my derived class I
have
new DataSource and DataMember properties (look at the pasted code).

Unfortunatelly this does not help. Am I doing something wrong?

I told you earlier that the repainting doesn't seem to work when I edited
the cells in the first view. This is actually not the case. From what I
can
see the problem occurs when I have selected a cell so that is is marked
(just
click in a cell in a datagrid and the text is highlited). When I am in
this
state and changes the data source (or datamember since I just shift
between
tables in the dataset), then that specific row with the selected cell is
not
repainted correctly in the new view.

Something that has helped in the past for a similar problem, is calling
Select() just before changing the DataSource, not sure if this wil help your
problem :

dataGrid1.Select();
dataGrid1.DataSource = ... ;

HTH,
Greetings
----------------------------------------------------------
// DataSource and DataMember property

public new object DataSource
{
get { return base.DataSource; }
set
{
this.SuspendLayout();

if (this.AutoSave) SaveChanges();

if (value.GetType().GetInterface("Offertering.Data.IDataList") != null)
{
base.DataSource = ((Offertering.Data.IDataList)value).DataSource;
((DataSet)this.DataSource).AcceptChanges();
}
else if (value.GetType() == typeof(DataSet))
{
base.DataSource = value;
((DataSet)this.DataSource).AcceptChanges();
}

// New property
AllowNewRow = AllowNewRow;

this.ResumeLayout(false);
}
}

public new string DataMember
{
get { return base.DataMember; }
set
{
BindingManagerBase bm = null;

this.SuspendLayout();
if (this.AutoSave) SaveChanges();

base.DataMember = value;

// If DataSource is a DataSet, then implement AllowRow for the
datamember
if (this.DataSource != null && this.DataSource.GetType() ==
typeof(DataSet))
{
// Implement the AllowNewRow setting for the new DataMember object
AllowNewRow = AllowNewRow;
}
}
}

--
/ ScubaD



TT (Tom Tempelaere) said:
Hi ScubaD,

:
Hi!

I derived a new class from a DataGrid control and added some
functionality
to it. Among other things a method that autosizes the columns
displayed.

When I change the data source and data member of the datagrid after
having
updated some data in it, it sometimes does not display correctly. What
happens is that some cells from the old view is visible in the new
view. I do
not know what is the problem, but my guess is that it has something to
do
with the repainting of the derived datagrid.

Do I need to do something in the derived data grid to get the painting
to
work correctly?
--
/ ScubaD

Before you change the data source, call "SuspendLayout" on the DataGrid
and
call ResumeLayout after making the changes. I believe that may solve your
issue.

If that doesn't work try the following:

BindingManagerBase bm = yourDataGrid.BindingContext [
yourDataGrid.DataSource,
yourDataGrid.DataMember
];
bm.SuspendBinding( );

// <-- modify the data source here

bm.ResumeBinding( );

Hope this helps,
 
G

Guest

Hi ScubaD,

// ScubaD said:
Hi TT!
I have tried using SuspendLayou as you sugested. In my derived class I have
new DataSource and DataMember properties (look at the pasted code).

Unfortunatelly this does not help. Am I doing something wrong?

I told you earlier that the repainting doesn't seem to work when I edited
the cells in the first view. This is actually not the case. From what I can
see the problem occurs when I have selected a cell so that is is marked (just
click in a cell in a datagrid and the text is highlited). When I am in this
state and changes the data source (or datamember since I just shift between
tables in the dataset), then that specific row with the selected cell is not
repainted correctly in the new view.

----------------------------------------------------------
// DataSource and DataMember property

public new object DataSource
{
get { return base.DataSource; }
set
{
this.SuspendLayout();

if (this.AutoSave) SaveChanges();

if (value.GetType().GetInterface("Offertering.Data.IDataList") != null)
{
base.DataSource = ((Offertering.Data.IDataList)value).DataSource;
((DataSet)this.DataSource).AcceptChanges();
}
else if (value.GetType() == typeof(DataSet))
{
base.DataSource = value;
((DataSet)this.DataSource).AcceptChanges();
}

// New property
AllowNewRow = AllowNewRow;

this.ResumeLayout(false);
}
}

public new string DataMember
{
get { return base.DataMember; }
set
{
BindingManagerBase bm = null;

this.SuspendLayout();
if (this.AutoSave) SaveChanges();

base.DataMember = value;

// If DataSource is a DataSet, then implement AllowRow for the datamember
if (this.DataSource != null && this.DataSource.GetType() ==
typeof(DataSet))
{
// Implement the AllowNewRow setting for the new DataMember object
AllowNewRow = AllowNewRow;
}
}
}

Sorry for the late response, but have your tried the second suggestion I
posted (ie using SuspendBinding & ResumeBinding on the binding manager?

On a sidenote, AVOID *new* methods! Do not overwrite, *ever*, unless
something forces you and you have no other way of accomplishing this.

Second, don't derive from DataGrid if it isn't necessary. Instead make a
custom control (System.Windows.Forms.UserControl) and drag a DataGrid onto
it. Of course this could be clumsy if you intend to "enhance" the DataGrid
with new functionality so that it is still a generic UI component.

I would definitely go for my advice n°2. It that doesn't work still, force a
repaint for the complete control (ie call Control.Invalidate method on the
DataGrid)

Hope this helps, and kind regards,
 
G

Guest

Hi TT,

I actually found what was wrong. The problem was that I used
'SuspendLayout()' on a higher level so I actually made two nested calls to
the function. This caused the DataGrid not to display correctly. Anyway,
thankyou for your help, it was good for me anyway because it got me thinking
a bit..

Just for curiosity, why shouldn't I derive from datagrid? Do you have bad
experiences of this or is it just not 'common practise'?

Thanks,
/ Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top