473,386 Members | 1,673 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,386 software developers and data experts.

Answer/HowTo : Accessing columns collection via the private autoGenColumnsArray property in a DataGrid using AutoGenerate

I have seen several people looking for a way to access the Columns
collection when using the AutoGenerate = true option. Some
people have gotten so far as to find the private autoGenColumnsArray
that has the information, but we as developers have no way to access
this information.

I have come up with a solution for the problem, (as I am sure many
others have) using reflection. Here is some sample code that will
print out the auto generated column names from a datagrid.

As stated, this code uses reflection. Reflection is slow. It might not
also be allowed depending on your system's security settings. Also, if
MS changes the internal structure of the DataGrid, this might break.
(We are breaking encapsulation)

Provided, is a function that will generically allow you to get the
value of any private member of a class using reflection.

Sorry about the formatting, I dont know a good way to format code for
usenet. Visual Studio should clean up the code just find for you tho.

<code>
DataGrid dg = new DataGrid();
dg.DataSource =new DataTable();// (Run some query or whatnot here)
dg.DataBind

ArrayList AutoGeneratedColumns
= (ArrayList) GetPrivateField(dg,"autoGenColumnsArray") ;

if (AutoGeneratedColumns!= null)
foreach (DataGridColumn CurrentColumn in AutoGeneratedColumns)
{
Response.Write(CurrentColumn.HeaderText);
}
public static object GetPrivateField(object PassedObject, string
FieldName)
{

object Field=null;

if (PassedObject == null)
throw new ArgumentNullException("PassedObject"
,"PassedObject must be an instantiated object.");

if (FieldName == null || FieldName.Trim() == "")
throw new ArgumentOutOfRangeException("FieldName"
,"Fieldname must be a non empty string.");

Type ObjectType = PassedObject.GetType();
System.Reflection.FieldInfo PrivateField =
ObjectType.GetField(FieldName
,System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.IgnoreCase);

if (PrivateField == null)
throw new ArgumentOutOfRangeException("FieldName"
, ObjectType.FullName + " does not have a field : " + FieldName +
".");

Field = PrivateField.GetValue(PassedObject);
return Field;
}
</code>
Nov 16 '05 #1
0 1660

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

Similar topics

0
by: Sarfraz Hooda | last post by:
Hi, I have a collection object which i am binding to a ASP.Net datagrid. Columns are not appearing in the proper sequence as I would like it to appear on datagrid. I know I can modify the...
2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
0
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerateColumns = true option. Some people have gotten so far as to find the private...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
0
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: James Zhuo | last post by:
Accessing Items in a Collection works with the as suggested by Kevin and Chris (thx) But when i tried to use the same technic with databinding within the aspx file, it doesn't seem to work. This...
2
by: damonf | last post by:
I'm currently trying to add an ASP hyperlink to a template column in a datagrid. The normal hyperlink column doesn't give me the ability to add attributes to the item. In my grid there are four...
2
by: Alex | last post by:
I would like to have one column in my DataGrid that content acts as a hyperlink. In addition to that I would like that the content is databound to a DataSet source. If I create a hyperlink...
1
by: Orlando Cavadini | last post by:
Hello I bind a collection of objects with Type AB (Class AB) to a DataGrid control as following: Public Class AB Private m_AA As Integer Private m_AB As Integer Private m_AC As Integer
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.