473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Extending a Collection indexer

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.
Nov 15 '05 #1
4 3409

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" <no****@hotmail.com>
| 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: <O1**************@TK2MSFTNGP11.phx.gbl>
| 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!TK2MSFTN GP11.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.
|
|
|

Nov 15 '05 #2
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[i]).Key == Key
return _columnSource[i]

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.
Nov 15 '05 #3

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==?=" <an*******@discussions.microsoft.com>
| References: <O1**************@TK2MSFTNGP11.phx.gbl>
<ry**************@cpmsftngxa06.phx.gbl>
| Subject: RE: Extending a Collection indexer
| Date: Fri, 14 Nov 2003 02:11:06 -0800
| Lines: 75
| Message-ID: <74**********************************@microsoft.co m>
| 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[i]).Key == Key)
return _columnSource[i];

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.
|

Nov 15 '05 #4
Thanks for your help.

Matt.

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:nh**************@cpmsftngxa06.phx.gbl...

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==?=" <an*******@discussions.microsoft.com>
| References: <O1**************@TK2MSFTNGP11.phx.gbl>
<ry**************@cpmsftngxa06.phx.gbl>
| Subject: RE: Extending a Collection indexer
| Date: Fri, 14 Nov 2003 02:11:06 -0800
| Lines: 75
| Message-ID: <74**********************************@microsoft.co m>
| 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[i]).Key == Key)
return _columnSource[i];

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.
|

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jamie_m_ | last post by:
I have a custom collection ... clFile that INHERITS from NameObjectCollectionBase the problem is, when I try to create an xmlserializer instance i get an error You must implement a default...
9
by: Gerald Lightsey | last post by:
I am doing some work that involves automating MS MapPoint 2002. The object model is documented in the context of Visual Basic 6.0. A typical example follows. 'Output first result of find...
0
by: panik | last post by:
Hi, I have a custom collection that implements CollectionBase. The collection is called Sensors and contains a list of Sensor objects. There is the usual index using an integer (Sensors). ...
9
by: Gary van der Merwe | last post by:
Hi I want to write my own collection object. I want it to have the following features. 1.. It must be strongly typed (to a Class that I have written). 2.. I should be able to add and...
4
by: Geordie | last post by:
Hi, I'm in the process of converting a production VS 2003 application to VS 2005. To simplify the conversion I'm converting a small piece at a time and then unit testing the code to confirm it...
2
by: Ghanashyam | last post by:
I have a custom collection (Dervided from CollectionBase) and it works pretty well when I consume it from C# console app.But it is not working if I return the collection from a web service.The IE...
1
by: SP | last post by:
I have created an abstract class inheriting from KeyedCollection<long, TItemto use as a base class for my collections. In some derived classes I am providing a new indexer property for the key...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
5
by: Nikolay Belyh | last post by:
I have created a "collection" in C# like this: namespace ClassLibrary1 { public class X {} public class Class1 { public ArrayList objs {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.