473,386 Members | 1,693 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 : Accessing the autoGenColumnsArray property in a DataGrid

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

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

Similar topics

11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
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: 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 AutoGenerateColumns = true option. Some people have gotten so far as to find the private...
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
3
by: Craig G | last post by:
i have a user control which is basically a datagrid, which has add/edit/delete buttons on the grid is there anyway of accessing the actual datagrid from the form itself? basically i want to...
1
by: Jasper Jones | last post by:
I'm really stuck on this, I just can't seem to figure it out. In the code below I read the item property of a Datagrid I have on my form. However, I get an error about the datagrid1 part saying...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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,...
0
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...

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.