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

RE: Derived DataGrid not displeyed correctly by _|\|_0$P

_|\|_0$P
Thu Dec 08 09:22:03 CST 2005

Hi ScubaD,

"ScubaD" wrote:
> 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,
--
Tom Tempelaere.


RE: Derived DataGrid not displeyed correctly by //

//
Sat Dec 10 05:44:02 CST 2005

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)" wrote:

> Hi ScubaD,
>
> "ScubaD" wrote:
> > 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,
> --
> Tom Tempelaere.
>

Re: Derived DataGrid not displeyed correctly by Bart

Bart
Sat Dec 10 06:30:30 CST 2005

hi,

"// ScubaD" <// ScubaD@discussions.microsoft.com> wrote in message
news:6E415690-E9DB-4189-9FB6-D109D4F649C6@microsoft.com...
> 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)" wrote:
>
>> Hi ScubaD,
>>
>> "ScubaD" wrote:
>> > 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,
>> --
>> Tom Tempelaere.
>>



Re: Derived DataGrid not displeyed correctly by ScubaD

ScubaD
Mon Dec 12 02:11:02 CST 2005

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

--
/ ScubaD


"Bart Mermuys" wrote:

> hi,
>
> "// ScubaD" <// ScubaD@discussions.microsoft.com> wrote in message
> news:6E415690-E9DB-4189-9FB6-D109D4F649C6@microsoft.com...
> > 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)" wrote:
> >
> >> Hi ScubaD,
> >>
> >> "ScubaD" wrote:
> >> > 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,
> >> --
> >> Tom Tempelaere.
> >>
>
>
>

RE: Derived DataGrid not displeyed correctly by _|\|_0$P

_|\|_0$P
Mon Dec 19 10:05:02 CST 2005

Hi ScubaD,

"// ScubaD" wrote:

> 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

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,
--
Tom Tempelaere.


RE: Derived DataGrid not displeyed correctly by ScubaD

ScubaD
Tue Dec 20 00:28:03 CST 2005

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
--
/ ScubaD


"TT (Tom Tempelaere)" wrote:

> Hi ScubaD,
>
> "// ScubaD" wrote:
>
> > 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
>
> 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,
> --
> Tom Tempelaere.
>