473,605 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.W ebControls.Data Grid which
works a treat, but I'd like to extend the DataGridColumnC ollection class
(property of DataGrid) just a fraction by allowing a string indexer. i.e.
rather than using

myDataGrid.Colu mns[7].Visible = false;

I'd like to be able to do this:

myDataGrid.Colu mns["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.Bou ndColumn :
System.Web.UI.W ebForms.BoundCo lumn) which also contain a property "Key" of
type string (from an interface).

DataGridColumnC ollection cannot be inherited.

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

TIA.

Matt.
Nov 15 '05 #1
4 3428

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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: user-3651.tcl21.dsl. pol.co.uk 81.77.206.67
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1991 89
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.W ebControls.Data Grid
which
| works a treat, but I'd like to extend the DataGridColumnC ollection class
| (property of DataGrid) just a fraction by allowing a string indexer. i.e.
| rather than using
|
| myDataGrid.Colu mns[7].Visible = false;
|
|
|
| I'd like to be able to do this:
|
|
|
| myDataGrid.Colu mns["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.Bou ndColumn :
| System.Web.UI.W ebForms.BoundCo lumn) which also contain a property "Key" of
| type string (from an interface).
|
|
|
| DataGridColumnC ollection cannot be inherited.
|
|
|
| Anyone have an idea on how to do this without creating a new property
(e.g.
| myColumns of type myDataGridColum nCollection).
|
|
|
| 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 DataGridCollect ion)

I've created a new property to by class myDataGrid

private myNamespace.Dat aGridColumnColl ection _dataGridColumn Collection

/// <summary
/// Gets columns by their key attribute
/// </summary
public virtual System.Web.UI.W ebControls.Data GridColumnColle ction ColumnsByKe

ge

if (_dataGridColum nCollection == null

_dataGridColumn Collection = new myNamespace.Dat aGridColumnColl ection()
_dataGridColumn Collection.Colu mnSource = Columns
return _dataGridColumn Collection

And the (proxy) collection class

/// <summary
/// myNamespace.Dat aGridColumnColl ection
/// </summary
public class DataGridColumnC ollection //: IEnumerable - removed to display less code

private System.Web.UI.W ebControls.Data GridColumnColle ction _columnSource

public DataGridColumnC ollection(

protected internal System.Web.UI.W ebControls.Data GridColumnColle ction ColumnSourc

get {return _columnSource;
set {_columnSource = value;
public System.Web.UI.W ebControls.Data GridColumn this[string Key

ge

for (int i=0; i<_columnSource .Count; i++
if (((IVPGridColum n)_columnSource[i]).Key == Key
return _columnSource[i]

return null


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

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.W ebControls.Data GridColumnColle ction

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.Colum ns[(int)ht["Balance"]].Visible=false;

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

If I still misunderstandin g 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+toVC8 AQIKSMcrk0va2dQ ==
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| From: "=?Utf-8?B?TWF0dA==?=" <an*******@disc ussions.microso ft.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************ *************** *******@microso ft.com>
| 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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: TK2MSFTCMTY1 10.40.1.180
| Path: cpmsftngxa06.ph x.gbl!cpmsftngx a10.phx.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1992 84
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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 DataGridCollect ion).

I've created a new property to by class myDataGrid:
private myNamespace.Dat aGridColumnColl ection _dataGridColumn Collection;

/// <summary>
/// Gets columns by their key attribute.
/// </summary>
public virtual System.Web.UI.W ebControls.Data GridColumnColle ction
ColumnsByKey
{
get
{
if (_dataGridColum nCollection == null)
{
_dataGridColumn Collection = new myNamespace.Dat aGridColumnColl ection();
_dataGridColumn Collection.Colu mnSource = Columns;
}

return _dataGridColumn Collection;
}
}
And the (proxy) collection class:

/// <summary>
/// myNamespace.Dat aGridColumnColl ection.
/// </summary>
public class DataGridColumnC ollection //: IEnumerable - removed to display
less code.
{
private System.Web.UI.W ebControls.Data GridColumnColle ction _columnSource;

public DataGridColumnC ollection()
{
}

protected internal System.Web.UI.W ebControls.Data GridColumnColle ction
ColumnSource
{
get {return _columnSource;}
set {_columnSource = value;}
}

public System.Web.UI.W ebControls.Data GridColumn this[string Key]
{
get
{
for (int i=0; i<_columnSource .Count; i++)
if (((IVPGridColum n)_columnSource[i]).Key == Key)
return _columnSource[i];

return null;
}
}
}
Now I have 2 properties for my DataGrid which return the same collection:
"Columns" and "ColumnsByK ey".

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.W ebControls.Data GridColumnColle ction.

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.mi crosoft.com> wrote in message
news:nh******** ******@cpmsftng xa06.phx.gbl...

Hi,

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

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

If I still misunderstandin g 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+toVC8 AQIKSMcrk0va2dQ ==
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| From: "=?Utf-8?B?TWF0dA==?=" <an*******@disc ussions.microso ft.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************ *************** *******@microso ft.com>
| 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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: TK2MSFTCMTY1 10.40.1.180
| Path: cpmsftngxa06.ph x.gbl!cpmsftngx a10.phx.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1992 84 | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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 DataGridCollect ion).

I've created a new property to by class myDataGrid:
private myNamespace.Dat aGridColumnColl ection _dataGridColumn Collection;

/// <summary>
/// Gets columns by their key attribute.
/// </summary>
public virtual System.Web.UI.W ebControls.Data GridColumnColle ction
ColumnsByKey
{
get
{
if (_dataGridColum nCollection == null)
{
_dataGridColumn Collection = new myNamespace.Dat aGridColumnColl ection();
_dataGridColumn Collection.Colu mnSource = Columns;
}

return _dataGridColumn Collection;
}
}
And the (proxy) collection class:

/// <summary>
/// myNamespace.Dat aGridColumnColl ection.
/// </summary>
public class DataGridColumnC ollection //: IEnumerable - removed to display
less code.
{
private System.Web.UI.W ebControls.Data GridColumnColle ction _columnSource;

public DataGridColumnC ollection()
{
}

protected internal System.Web.UI.W ebControls.Data GridColumnColle ction
ColumnSource
{
get {return _columnSource;}
set {_columnSource = value;}
}

public System.Web.UI.W ebControls.Data GridColumn this[string Key]
{
get
{
for (int i=0; i<_columnSource .Count; i++)
if (((IVPGridColum n)_columnSource[i]).Key == Key)
return _columnSource[i];

return null;
}
}
}
Now I have 2 properties for my DataGrid which return the same collection:
"Columns" and "ColumnsByK ey".

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.W ebControls.Data GridColumnColle ction.

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
12858
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 accessor on brWAP.clFile because it inherits from ICollectio I must be having a thick day because I dont have a clue what the error message means. (I have written a defaut property called ITEM in the clFile class 'Classes to abstract a FILE entity...
9
8008
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 search Set objFindResults = objApp.ActiveMap.FindAddressResults("One Microsoft Way", "Redmond", , "WA", , geoCountryUnitedStates) MsgBox "The first item in the find list is: " _ + objFindResults.Item(1).Name
0
4062
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). Is there a way of using a custom indexer (for example Sensors) that does *not* need to loop through each item in the collection and compare them?
9
1671
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 remove items dynamically. 3.. It should have an indexer. This will have a string parameter. The
4
1680
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 converted correctly. The application consists of 10 seperate projects. I have hit a problem with converting the core object model project. The object model consists of hierarchical data some of which implement the IBinding and IBindingList...
2
1060
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 complains about( You must implement a default accessor as it inherits from ICollection). I want to now what are the methods need to be implemented for that collection.Any help with sample code is highly appreciated. Thanks
1
3030
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 (long). I need to access my collection by index and by key however the new long indexer is used for BOTH int and long, i.e. myCollection will use the long indexer NOT the base classes int indexer. Why is that? The workaround is to also define a new...
7
5500
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, would return the value of obj because it returns the index of the item at position obj. Thoughts I had were to let the default method perform the default behavior and then create an overload as IndexOf(int index, ProductSearchCriteria criteria)...
5
3601
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
8004
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1271
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.