Extending a Collection indexer

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

Hi,

I've been thinking about how to do this, but can't think of a solution.

I have a class that is derived from System.Web.UI.WebControls.DataGrid which
works a treat, but I'd like to extend the DataGridColumnCollection class
(property of DataGrid) just a fraction by allowing a string indexer. i.e.
rather than using

myDataGrid.Columns[7].Visible = false;



I'd like to be able to do this:



myDataGrid.Columns["Balance"].Visible = false;



Background: We have a design where only certain columns are shown depending
on the currently logged in user's roles. Management tend to keep changing
the layout by moving columns around, creating havoc in the code behind as
hiding/showing columns is currently done by using the indexer as shown
above. Moving column 7 to column 9 causes problems if you forget to change
the role checks for columns above 8 etc. I've introduced int constants now
for column names to help a bit, but still can create problems if not done
correctly. It would be nicer to use "Key" names. All columns of myDataGrid
derive from their parent (eg myNamespace.BoundColumn :
System.Web.UI.WebForms.BoundColumn) which also contain a property "Key" of
type string (from an interface).



DataGridColumnCollection cannot be inherited.



Anyone have an idea on how to do this without creating a new property (e.g.
myColumns of type myDataGridColumnCollection).



TIA.

Matt.
 
Hi Matt,

Thanks for posting in this group.
I think when initialize, you can store the key/value pairs in a hashtable,
the key is you wanted string. Then you can use string through the hashtable
to get the index of the column.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Matt" <[email protected]>
| Subject: Extending a Collection indexer
| Date: Thu, 13 Nov 2003 22:17:49 -0000
| Lines: 49
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: user-3651.tcl21.dsl.pol.co.uk 81.77.206.67
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:199189
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi,
|
| I've been thinking about how to do this, but can't think of a solution.
|
| I have a class that is derived from System.Web.UI.WebControls.DataGrid
which
| works a treat, but I'd like to extend the DataGridColumnCollection class
| (property of DataGrid) just a fraction by allowing a string indexer. i.e.
| rather than using
|
| myDataGrid.Columns[7].Visible = false;
|
|
|
| I'd like to be able to do this:
|
|
|
| myDataGrid.Columns["Balance"].Visible = false;
|
|
|
| Background: We have a design where only certain columns are shown
depending
| on the currently logged in user's roles. Management tend to keep changing
| the layout by moving columns around, creating havoc in the code behind as
| hiding/showing columns is currently done by using the indexer as shown
| above. Moving column 7 to column 9 causes problems if you forget to
change
| the role checks for columns above 8 etc. I've introduced int constants
now
| for column names to help a bit, but still can create problems if not done
| correctly. It would be nicer to use "Key" names. All columns of
myDataGrid
| derive from their parent (eg myNamespace.BoundColumn :
| System.Web.UI.WebForms.BoundColumn) which also contain a property "Key" of
| type string (from an interface).
|
|
|
| DataGridColumnCollection cannot be inherited.
|
|
|
| Anyone have an idea on how to do this without creating a new property
(e.g.
| myColumns of type myDataGridColumnCollection).
|
|
|
| TIA.
|
| Matt.
|
|
|
 
Hi Jeffrey

Thanks for your response, but I don't fully understand what you imply

I could create a new Hashtable property with the string/value pairs, but his is no different really from just using int constants

What I'm trying to acheive is to be able to extend the current "Columns" property (of type DataGridCollection)

I've created a new property to by class myDataGrid

private myNamespace.DataGridColumnCollection _dataGridColumnCollection

/// <summary
/// Gets columns by their key attribute
/// </summary
public virtual System.Web.UI.WebControls.DataGridColumnCollection ColumnsByKe

ge

if (_dataGridColumnCollection == null

_dataGridColumnCollection = new myNamespace.DataGridColumnCollection()
_dataGridColumnCollection.ColumnSource = Columns


return _dataGridColumnCollection



And the (proxy) collection class

/// <summary
/// myNamespace.DataGridColumnCollection
/// </summary
public class DataGridColumnCollection //: IEnumerable - removed to display less code

private System.Web.UI.WebControls.DataGridColumnCollection _columnSource

public DataGridColumnCollection(



protected internal System.Web.UI.WebControls.DataGridColumnCollection ColumnSourc

get {return _columnSource;
set {_columnSource = value;


public System.Web.UI.WebControls.DataGridColumn this[string Key

ge

for (int i=0; i<_columnSource.Count; i++
if (((IVPGridColumn)_columnSource).Key == Key
return _columnSource

return null




Now I have 2 properties for my DataGrid which return the same collection: "Columns" and "ColumnsByKey"

What I really want is to extend the orignal property "Columns" by creating a property this[string Key] for it. However I can't derive from System.Web.UI.WebControls.DataGridColumnCollection

Is it possible to do this

I hope this makes sense

TIA

Matt.
 
Hi,

Sorry, based on my understanding, you can do like this:
Hashtable ht=new Hashtable();
ht.Add("Balance",7);
DataGrid1.Columns[(int)ht["Balance"]].Visible=false;

I think this is equivalent to DataGrid1.Columns[7].Visible=false;

If I still misunderstanding you, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Thread-Topic: Extending a Collection indexer
| thread-index: AcOql52oJ+toVC8AQIKSMcrk0va2dQ==
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| From: "=?Utf-8?B?TWF0dA==?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Extending a Collection indexer
| Date: Fri, 14 Nov 2003 02:11:06 -0800
| Lines: 75
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: TK2MSFTCMTY1 10.40.1.180
| Path: cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:199284
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Jeffrey,

Thanks for your response, but I don't fully understand what you imply.

I could create a new Hashtable property with the string/value pairs, but
his is no different really from just using int constants.

What I'm trying to acheive is to be able to extend the current "Columns"
property (of type DataGridCollection).

I've created a new property to by class myDataGrid:


private myNamespace.DataGridColumnCollection _dataGridColumnCollection;

/// <summary>
/// Gets columns by their key attribute.
/// </summary>
public virtual System.Web.UI.WebControls.DataGridColumnCollection
ColumnsByKey
{
get
{
if (_dataGridColumnCollection == null)
{
_dataGridColumnCollection = new myNamespace.DataGridColumnCollection();
_dataGridColumnCollection.ColumnSource = Columns;
}

return _dataGridColumnCollection;
}
}


And the (proxy) collection class:

/// <summary>
/// myNamespace.DataGridColumnCollection.
/// </summary>
public class DataGridColumnCollection //: IEnumerable - removed to display
less code.
{
private System.Web.UI.WebControls.DataGridColumnCollection _columnSource;

public DataGridColumnCollection()
{
}

protected internal System.Web.UI.WebControls.DataGridColumnCollection
ColumnSource
{
get {return _columnSource;}
set {_columnSource = value;}
}

public System.Web.UI.WebControls.DataGridColumn this[string Key]
{
get
{
for (int i=0; i<_columnSource.Count; i++)
if (((IVPGridColumn)_columnSource).Key == Key)
return _columnSource;

return null;
}
}
}


Now I have 2 properties for my DataGrid which return the same collection:
"Columns" and "ColumnsByKey".

What I really want is to extend the orignal property "Columns" by creating
a property this[string Key] for it. However I can't derive from
System.Web.UI.WebControls.DataGridColumnCollection.

Is it possible to do this?

I hope this makes sense.

TIA,

Matt.
|
 
Thanks for your help.

Matt.

"Jeffrey Tan[MSFT]" said:
Hi,

Sorry, based on my understanding, you can do like this:
Hashtable ht=new Hashtable();
ht.Add("Balance",7);
DataGrid1.Columns[(int)ht["Balance"]].Visible=false;

I think this is equivalent to DataGrid1.Columns[7].Visible=false;

If I still misunderstanding you, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Thread-Topic: Extending a Collection indexer
| thread-index: AcOql52oJ+toVC8AQIKSMcrk0va2dQ==
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| From: "=?Utf-8?B?TWF0dA==?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Extending a Collection indexer
| Date: Fri, 14 Nov 2003 02:11:06 -0800
| Lines: 75
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: TK2MSFTCMTY1 10.40.1.180
| Path: cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:199284
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Jeffrey,

Thanks for your response, but I don't fully understand what you imply.

I could create a new Hashtable property with the string/value pairs, but
his is no different really from just using int constants.

What I'm trying to acheive is to be able to extend the current "Columns"
property (of type DataGridCollection).

I've created a new property to by class myDataGrid:


private myNamespace.DataGridColumnCollection _dataGridColumnCollection;

/// <summary>
/// Gets columns by their key attribute.
/// </summary>
public virtual System.Web.UI.WebControls.DataGridColumnCollection
ColumnsByKey
{
get
{
if (_dataGridColumnCollection == null)
{
_dataGridColumnCollection = new myNamespace.DataGridColumnCollection();
_dataGridColumnCollection.ColumnSource = Columns;
}

return _dataGridColumnCollection;
}
}


And the (proxy) collection class:

/// <summary>
/// myNamespace.DataGridColumnCollection.
/// </summary>
public class DataGridColumnCollection //: IEnumerable - removed to display
less code.
{
private System.Web.UI.WebControls.DataGridColumnCollection _columnSource;

public DataGridColumnCollection()
{
}

protected internal System.Web.UI.WebControls.DataGridColumnCollection
ColumnSource
{
get {return _columnSource;}
set {_columnSource = value;}
}

public System.Web.UI.WebControls.DataGridColumn this[string Key]
{
get
{
for (int i=0; i<_columnSource.Count; i++)
if (((IVPGridColumn)_columnSource).Key == Key)
return _columnSource;

return null;
}
}
}


Now I have 2 properties for my DataGrid which return the same collection:
"Columns" and "ColumnsByKey".

What I really want is to extend the orignal property "Columns" by creating
a property this[string Key] for it. However I can't derive from
System.Web.UI.WebControls.DataGridColumnCollection.

Is it possible to do this?

I hope this makes sense.

TIA,

Matt.
|
 
Back
Top