1

I have an ASPX page (with VB Codebehind). I would like to extend the GridView class to show the header / footer when no rows are returned.

I found a C# example online (link) (source). However, I cannot convert it to VB because it uses typed events (which are not legal in VB).

I have tried several free C# to VB.NET converters online, but none have worked.

Please convert the example to VB.NET or provide an alternate method of extending the GridView class.

Notes / Difficulties:

  1. If you get an error with DataView objects, specify the type as System.Data.DataView and the type comparison could be the following: If data.[GetType]() Is GetType(System.Data.DataView) Then

  2. Since the event MustAddARow cannot have a type in VB (and RaiseEvent event doesn't have a return value), how can I compare it to Nothing in the function OnMustAddARow()?

EDIT:

The following is a sample with (hopefully) relevant code to help answer the question.

namespace AlwaysShowHeaderFooter {
    public delegate IEnumerable MustAddARowHandler(IEnumerable data);

    public class GridViewAlwaysShow : GridView {
           // Various member functions omitted //
           protected IEnumerable OnMustAddARow(IEnumerable data) {
            if (MustAddARow == null) {
                throw new NullReferenceException("The datasource has no rows. You must handle the \"MustAddARow\" Event.");
            }
            return MustAddARow(data);
        }

        public event MustAddARowHandler MustAddARow;
    }
}
5
  • 1
    I would help if you included an example of a 'typed event' in the question itself. Commented Apr 22, 2010 at 20:42
  • 2
    If you compile the code and look at in Reflector you can switch to VB.Net view. No need for online converters. Commented Apr 22, 2010 at 20:42
  • @Mikael: I can't get the Reflector to convert the code. Submit the equivalent working VB code as an answer and I'll accept. Commented Apr 23, 2010 at 13:03
  • @Steven, I tried to give it a go, but my VB skills and patience today is not up to it :) But I'm sure it can be converted, but you have to change the code a bit to get it to run. A Reflector export just won't cut it. Maybe it's easier to create this part in C#, and link it. Commented Apr 25, 2010 at 17:38
  • @Mikael: How do I link it? Might as well submit as an answer. Commented Apr 27, 2010 at 15:29

4 Answers 4

1

As I said in my comment the code will not convert to VB.Net "magically". It will require working thru it to get it to compile properly.

The easiest would be to compile the the C# code as a library.

  1. Create a new project (C# Class Library) and name it "AlwaysShowHeaderFooter"
  2. Move the files from App_Code over to the new project
  3. Add a reference to System.Web and System.Configuration
  4. Add a reference in your web project to either the compiled dll's of "AlwaysShowHeaderFooter", or add the project itself as a reference if you have it in the same solution.
  5. Switch out <%@ Register TagPrefix="Custom" Namespace="AlwaysShowHeaderFooter" %> with <%@ Register Assembly="AlwaysShowHeaderFooter" Namespace="AlwaysShowHeaderFooter" TagPrefix="Custom" %>

Now you have split the control out to it's own project which can be referenced in any .Net project.

Sign up to request clarification or add additional context in comments.

2 Comments

I built the DLL, added the DLL as a reference, added your "<%@ Register Assembly..." line with the others at the top of my Default.aspx. However, the control is not shown in IntelliSense when I start typing "<cu" (which should be between <code> and <del>. What am I doing wrong?
I got it. I added <add assembly="AlwaysShowHeaderFooter"/> to my web.config inside <compilation ...> <assemblies>. Thank you for your help!
1

Please see here.

There is no exact equivalent to this for VB.

Use a 'Sub' delegate with a 'ByRef' parameter instead.

A follow-up answer gives an example that works, but explains why it's a bad idea.

Comments

1

Concerning 1, the cleanest VB way would be

If TypeOf data Is System.Data.DataView Then

Comments

0

Regarding 2. I think events in VB.Net can pass ByRef parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.