Connecting Tech Pros Worldwide Forums | Help | Site Map

want override, but stuck on the inheritance

BeemerBiker's Avatar
Member
 
Join Date: Jul 2008
Location: San Antonio, Texas
Posts: 68
#1: Mar 18 '09
I want to use the C# example shown here

Ok, I have done simple overrides before so I coded up

protected override Auto...

well, I didnt even get that far as intellisense made it clear that AutoGeneratedField is not part of my class ..

public partial class printit : System.Web.UI.Page


So I tried adding "DetailView" to the class so I could inherit it (this was just a wild guess that once worked in c++ for me)
Expand|Select|Wrap|Line Numbers
  1.    public partial class printit : System.Web.UI.Page, System.Web.UI.WebControls.DetailsView
  2. {
  3.  
Anyway, the above does not work giving me a cannot have multiple base classes.

There is a really good example of what I want to do here but the author leaves the description on how to "use it" for a later date.

Question: What do I put in to my C# page that willl cause the override to be fired? I assume I have to inherit the class that does the firing and I do not know enough about inheritance to do this.

I have set GridView1.AutoGenerateColumns=True and coded up the following, but the override is never called.

Expand|Select|Wrap|Line Numbers
  1.  public class MyDetailsView : DetailsView
  2.     {
  3.         public MyDetailsView()
  4.         {
  5.         }
  6.         protected override AutoGeneratedField CreateAutoGeneratedRow(AutoGeneratedFieldProperties fieldProperties)
  7.         {
  8.             AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
  9.             return field;
  10.         }
  11.  
  12.     }
  13.  
  14.     public partial class printit : System.Web.UI.Page
  15.     {
  16. ...rest of my program...
  17.  

===wait wait wait===

ok, just realized I should have used MyGridView instead of the above "MyClass" and then used MyGridView when createing GridView1 . That gets me a bunch of new overrides though not the one I am looking for. However, I am in the right direction.

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: Mar 18 '09

re: want override, but stuck on the inheritance


What exactly are you trying to accomplish?

You may be having problems with inheriting the AutoGeneratedField class because the specifications (that you linked us to) says the following:
Quote:

Originally Posted by article

AutoGeneratedField Class

Represents an automatically generated field in a data-bound control. This class cannot be inherited.

I have a feeling that you'll be interested in handling the GridView RowDataBound Event.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#3: Mar 18 '09

re: want override, but stuck on the inheritance


If you are going to Override it in the child class, you must mark it Virtual in the Parent
BeemerBiker's Avatar
Member
 
Join Date: Jul 2008
Location: San Antonio, Texas
Posts: 68
#4: Mar 19 '09

re: want override, but stuck on the inheritance


Quote:

Originally Posted by Frinavale View Post

What exactly are you trying to accomplish?

You may be having problems with inheriting the AutoGeneratedField class because the specifications (that you linked us to) says the following:


I have a feeling that you'll be interested in handling the GridView RowDataBound Event.

Frina: there is something wrong with the format of your post and I cannot quote it exactly as you posted it as you can see above.

Q: What exactly are you trying to accomplish?

I have a lookup table of Field names, Column captions, Column widths and DataFormatStrings. At RowCreated I lookup the fieldname and substitute my caption which could include <br>, set the width, and arrange for the correct format to be displayed. This actually worked decently except for a minor hack with the dataformatstring. The real problem occurred when I set AllowSorting to be true: Header.text no longer had the field names. I found the fieldnames at ((GridView)sender)._autoGenFieldProps but it was private, showed up only in the watch, and my entire lookup scheme that had worked perfectly, then failed. Not only was there nothing to lookup, but I discovered that if I stuck a caption at Header.Text, as I was wont to do, then the sort would not work as the linkbutton was not created.

Q: Specification says ..this class cannot be inherited..

Yea, not only that but it is a "sealed" class (FWIW)

Q: I have a feeling...

OK - It was a gut wrenching feeling, but I did get it all working thanks to an old bytes thread here I did learn something, but it was a huge hack by the time I got thru.

My original post was to find out how to set up the inheritance so my override worked. Something like the following worked:

Expand|Select|Wrap|Line Numbers
  1. public class MyGridView : GridView
  2. {
  3.     protected override AutoGeneratedField  CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
  4.     {
  5.         AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
  6.          return field;
  7.     }
  8. }
  9. ..
  10. ..
  11. MyGridView GridView1 = new MyGridView()
  12.  
In the above override I was able to lookup the field name and set the captions and still get the sort to work. Unfortuately, setting DataFormatString ended up as a true hack in every sense of the word and I had to use that reflection mechanism mentioned in an old thread in this forum.
Reply

Tags
autogeneratecolumns, gridview, inheritance, override