473,769 Members | 3,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagrid columns using AutoGenerate via the autoGenColumnsA rray private member array collection (reflection)

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 autoGenColumnsA rray
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 AutoGeneratedCo lumns
= (ArrayList) GetPrivateField (dg,"autoGenCol umnsArray") ;

if (AutoGeneratedC olumns!= null)
foreach (DataGridColumn CurrentColumn in AutoGeneratedCo lumns)
{
Response.Write( CurrentColumn.H eaderText);
}
public static object GetPrivateField (object PassedObject, string
FieldName)
{

object Field=null;

if (PassedObject == null)
throw new ArgumentNullExc eption("PassedO bject"
,"PassedObje ct must be an instantiated object.");

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

Type ObjectType = PassedObject.Ge tType();
System.Reflecti on.FieldInfo PrivateField =
ObjectType.GetF ield(FieldName
,System.Reflect ion.BindingFlag s.Instance
| System.Reflecti on.BindingFlags .NonPublic
| System.Reflecti on.BindingFlags .Public
| System.Reflecti on.BindingFlags .IgnoreCase);

if (PrivateField == null)
throw new ArgumentOutOfRa ngeException("F ieldName"
, ObjectType.Full Name + " does not have a field : " + FieldName +
".");

Field = PrivateField.Ge tValue(PassedOb ject);
return Field;
}
</code>
Nov 16 '05 #1
4 4522
With autogen=true, why not simply add to the Table you are binding to. This
can be extended quite efficiently in XML or in the ADO.NET objects. When
autogen=false, you need to explicitly expand the DataGrid, as well.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** *************** ***
Think outside the box!
*************** *************** *************** *************** ***
"Jason Coyne Gaijin42" <go************ *******@sneakem ail.com> wrote in
message news:98******** *************** ***@posting.goo gle.com...
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 autoGenColumnsA rray
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 AutoGeneratedCo lumns
= (ArrayList) GetPrivateField (dg,"autoGenCol umnsArray") ;

if (AutoGeneratedC olumns!= null)
foreach (DataGridColumn CurrentColumn in AutoGeneratedCo lumns)
{
Response.Write( CurrentColumn.H eaderText);
}
public static object GetPrivateField (object PassedObject, string
FieldName)
{

object Field=null;

if (PassedObject == null)
throw new ArgumentNullExc eption("PassedO bject"
,"PassedObje ct must be an instantiated object.");

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

Type ObjectType = PassedObject.Ge tType();
System.Reflecti on.FieldInfo PrivateField =
ObjectType.GetF ield(FieldName
,System.Reflect ion.BindingFlag s.Instance
| System.Reflecti on.BindingFlags .NonPublic
| System.Reflecti on.BindingFlags .Public
| System.Reflecti on.BindingFlags .IgnoreCase);

if (PrivateField == null)
throw new ArgumentOutOfRa ngeException("F ieldName"
, ObjectType.Full Name + " does not have a field : " + FieldName +
".");

Field = PrivateField.Ge tValue(PassedOb ject);
return Field;
}
</code>

Nov 16 '05 #2
With autogen=true, why not simply add to the Table you are binding to. This
can be extended quite efficiently in XML or in the ADO.NET objects. When
autogen=false, you need to explicitly expand the DataGrid, as well.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** *************** ***
Think outside the box!
*************** *************** *************** *************** ***
"Jason Coyne Gaijin42" <go************ *******@sneakem ail.com> wrote in
message news:98******** *************** ***@posting.goo gle.com...
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 autoGenColumnsA rray
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 AutoGeneratedCo lumns
= (ArrayList) GetPrivateField (dg,"autoGenCol umnsArray") ;

if (AutoGeneratedC olumns!= null)
foreach (DataGridColumn CurrentColumn in AutoGeneratedCo lumns)
{
Response.Write( CurrentColumn.H eaderText);
}
public static object GetPrivateField (object PassedObject, string
FieldName)
{

object Field=null;

if (PassedObject == null)
throw new ArgumentNullExc eption("PassedO bject"
,"PassedObje ct must be an instantiated object.");

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

Type ObjectType = PassedObject.Ge tType();
System.Reflecti on.FieldInfo PrivateField =
ObjectType.GetF ield(FieldName
,System.Reflect ion.BindingFlag s.Instance
| System.Reflecti on.BindingFlags .NonPublic
| System.Reflecti on.BindingFlags .Public
| System.Reflecti on.BindingFlags .IgnoreCase);

if (PrivateField == null)
throw new ArgumentOutOfRa ngeException("F ieldName"
, ObjectType.Full Name + " does not have a field : " + FieldName +
".");

Field = PrivateField.Ge tValue(PassedOb ject);
return Field;
}
</code>

Nov 16 '05 #3
The purpose of the code was not to change the columns collection, but
to iterate through it.

In my particular case, I was writing a utility that would take any
datagrid, and output an excel worksheet that contained the contents of
that datagrid. I could get all the data fine, by looping through the
Items collection, but to get the column headings, I looped through the
columns array. However for autogenerated columns, they did not exist
in the columns array, only in the autogencolumnsa rray and there is no
way to access that. So I wrote a way to access that.

It was possible to iterate through the underlying DataSource object,
but the code was rather clunky. The autoGenColumns array was much
cleaner.

"Cowboy \(Gregory A. Beamer\) [MVP]" <No************ @comcast.netNoS pamM> wrote in message news:<O3******* *******@TK2MSFT NGP11.phx.gbl>. ..
With autogen=true, why not simply add to the Table you are binding to. This
can be extended quite efficiently in XML or in the ADO.NET objects. When
autogen=false, you need to explicitly expand the DataGrid, as well.

Nov 16 '05 #4
The purpose of the code was not to change the columns collection, but
to iterate through it.

In my particular case, I was writing a utility that would take any
datagrid, and output an excel worksheet that contained the contents of
that datagrid. I could get all the data fine, by looping through the
Items collection, but to get the column headings, I looped through the
columns array. However for autogenerated columns, they did not exist
in the columns array, only in the autogencolumnsa rray and there is no
way to access that. So I wrote a way to access that.

It was possible to iterate through the underlying DataSource object,
but the code was rather clunky. The autoGenColumns array was much
cleaner.

"Cowboy \(Gregory A. Beamer\) [MVP]" <No************ @comcast.netNoS pamM> wrote in message news:<O3******* *******@TK2MSFT NGP11.phx.gbl>. ..
With autogen=true, why not simply add to the Table you are binding to. This
can be extended quite efficiently in XML or in the ADO.NET objects. When
autogen=false, you need to explicitly expand the DataGrid, as well.

Nov 16 '05 #5

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

Similar topics

2
2763
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. The problem is the article is in VisualBasic. I already get the collection to be recognized as a Data Source by the IDE. It populated the DataGrid correctly from the fields on the items object of the collection, but I can't get the DataGrid to...
0
1407
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 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...
0
1722
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 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...
2
864
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 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...
0
2033
by: Wiktor Zychla | last post by:
Hello, I am thinking of a general solution to the specific issue and I hope someone has some more experience on that. Up to now I use the ListView as the primary control for collections of items. Unfortunately, the ListView behaves sluggishly with large amounts of data. That's why I think of using DataGrid instead. The DataGrid in my application should be databinded to a memory
2
4333
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know how to use this code? please help! http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20862953.html
2
2507
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is the OID of Person), the information of the person matching the selected OID is shown in the Textboxes (Name, Address, id, etc) and the courses this person is taken are shown in a DataGrid (course name, price, etc.). This is working well so far, I...
3
4885
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
2
6206
by: gh0st54 | last post by:
hi still working on changing my data grid headers using resource files the thing is i always gat a count of 0 for my datagrid columns count, when will i get the columns count and when should i change the headers ?? thanks private void Page_Load(object sender, System.EventArgs e)
0
1535
by: arlie_maija | last post by:
Hey - I'm writing a control that contains a DataGrid, and I'm unable to get the update event to fire. When I click the update link, the edit event fires. heres the details... my control overrides CreateChildControls and dynamically creates the DataGrid, creates an EditCommandColumn which it adds to the
0
9583
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
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10039
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
9990
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
8869
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
7406
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
5297
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2814
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.