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

dotnet-framework

    Next

  • 1. Framework
    When is V2.0 of the framework to be released ? Thanks Henry
  • 2. Microsoft's DNP for Oracle. HELP!! (sorry for the x-post)
    Hello all, I am currently working on a project that uses MS's .net provider for Oracle. Now, here is where it gets WEIRD!!! for some reason when we run it in VS.NET's IDE it is fine, a previous build deployed (xcopy) onto a test PC is fine. However; when we have recently but a new build on it doesn't work. The only thing (and I ain't buying it) that my work mate says it is down to is that the path of the EXE has brackets in it!!! When we remove the brackets it is fine (something ain't right)!!!! Has anyone had a similar scenario or a explanation for this bizarre behaviour???? Regards Alan Seunarayan
  • 3. List of countries
    Anyone know how I can access the "inbuilt" list of countries in Windows? Thanks. VV
  • 4. Does .NET provide any mechanism to replace ActiveX controls on web???
    Hi, Is there any mechanism in .NET Framework which can help me to replace activex controls on my web page?? As activex controls need to be downloaded on the client machine, and my clients are not interested in downloading any such binaries to their machines for a bunch of reasons, including the consumption of bandwidth as a strong one. Like, by the virtue of .NET framework, can I acheive the functionality of a "Tree Control" on my web page, without using/downloading the ActiveX Tree Control?? Looking forward ... Regards, Muhammad Shuaib

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

Postby googlegroups-gaijin42 » Wed, 07 Apr 2004 05:45:28 GMT

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>

Re: Datagrid columns using AutoGenerate via the autoGenColumnsArray private member array collection (reflection)

Postby Cowboy (Gregory A. Beamer)[MVP] » Wed, 07 Apr 2004 06:47:23 GMT

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" < XXXX@XXXXX.COM > wrote in





Re: Datagrid columns using AutoGenerate via the autoGenColumnsArray private member array collection (reflection)

Postby googlegroups-gaijin42 » Wed, 07 Apr 2004 22:24:39 GMT

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.






Similar Threads:

1.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>

2.Having problem getting private members using reflection.

I have an ASP.NET application that is calling a custom class that is trying
to parse all of the members of my Page object using Type.GetMembers().  The
problem that I am having is that private members are not returned.  I did
some digging and the MSDN documentation states that the caller must have
ReflectionPermission in order to get the private members of a class.  I am a
little unfamiliar with this stipulation.  I have checked the docs on
ReflectionPermission, but the examples do not make much sense.  Could
someone please clarify on what I need to do in order for my code to be able
to parse private members of my Page using reflection?

-- 
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


3.How to get private class members using reflection

Hi,

I'm trying to get some private class members using reflection, but am having 
trouble:  Example

using System;
using System.Reflection;

public class Customer
{
   private string _fname;

    public string FirstName
    {
       get	{return _fname;}
       set	{_fname = value;}
    }

    private void GetMyMembers ()
    {
         Type t = this.GetType();
         MemberInfo[] mInfo = t.GetMembers();

     }

     foreach(MemberInfo m in mInfo)
     {
         members += m.Name + " ";
         Console.WriteLine(m.Name);
     }

}


my output shows 
get_FirstName
set_FirstName

what I want is _fname.

Any ideas?

Thanks,

Opa


4.Adding Focus to new row to a DataGrid using Autogenerated Columns

5.How to use Reflection to access private members?

I have a Sky class with the private property Isblue

I am trying to access this through reflection (NOT through the Sky_Accessor 
class vstudio can create):
var mySky=new Sky();
var skyType=typeof(mySky);
var skyIsBlueProp=skyType.GetProperty("IsBlue");
var skyIsBluePropGetter=skyIsBlueProp.GetGetMethod();

the last line fails because skyIsBlueProp is null.
Changing the IsBlue property to public solves the issue.
How can I access IsBlue even when it is private?

6. reposted: accessing private members through reflection in vstu

7. reposted: accessing private members through reflection in vstudio

8. Reflection, hiding private members?



Return to dotnet-framework

 

Who is online

Users browsing this forum: No registered users and 5 guest