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> 4 4482
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*******************@sneakemail.com> wrote in
message news:98**************************@posting.google.c om... 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>
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*******************@sneakemail.com> wrote in
message news:98**************************@posting.google.c om... 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>
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 autogencolumnsarray 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.netNoSpamM> wrote in message news:<O3**************@TK2MSFTNGP11.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.
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 autogencolumnsarray 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.netNoSpamM> wrote in message news:<O3**************@TK2MSFTNGP11.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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: 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,...
|
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: 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...
|
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,...
| |