473,779 Members | 2,089 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 4524
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
2764
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
1723
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
2034
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
4334
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
6209
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
9636
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
9474
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
10139
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...
0
9931
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
8961
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
7485
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
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
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...
3
2869
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.