locked
VS 2010 problem with GridView control RRS feed

  • Question

  • I have a user control I'm building in VS 2010 (VB).  I can place the control on the form and it works just fine.  Now I want to add a GridView on the control...and it works just fine.  Normally, when I want to add a selected index changed event I double click the GridView control and visual studio takes me to the code-behind page and stubs in the default event for this control, and parks my cursor there to begin coding.  Here's the problem:  The compiler is saying "Handles clause requires WithEvents variable defined in the containing type....

    If I add this line:

     Protected WithEvents GridView1 As GridView
    


    The compiler then complains that GridView1 is already declared as Protected WithEvents!

    I'm stuck in a Catch-22 nightmare! Help!

    Joseph Everett

    • Edited by hannahsguy Monday, September 12, 2011 1:38 PM
    Monday, September 12, 2011 1:36 PM

Answers

  • Go to your code behind page and look above the code, there are two dropdowns aside from each other. Select in the left one the your GridView1, 

    Select that than you go on the same line the dropdown right and select the selectedIndexchange event.


    Success
    Cor
    • Edited by Cor Ligthert Monday, September 12, 2011 2:31 PM
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:47 PM
    Monday, September 12, 2011 2:31 PM
  •  

     

    in that case, can you post the markup for that control? in ASP, controls get defined in the page markup (source view), and their markup configures how they use code-behind events.

     

    here is a sample. note OnRowCommand (another event like SelectedIndexChanged):

      <asp:GridView ID="gvUsers" runat="server" 
         AutoGenerateColumns="False" 
         GridLines="None" 
         AllowPaging="true" 
         CssClass="mGrid" 
         PagerStyle-CssClass="pgr" 
         AlternatingRowStyle-CssClass="alt"
         AutoGenerateSelectButton="true"
         AllowSorting="true"
         datakeynames="UserID"
         onrowcommand="UserGridView_RowCommand"
         >

    and in my codebehind I have (Note, there is no handles clause, the markup handles that):

    Public Sub UserGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)


    • Edited by doas777 Monday, September 12, 2011 4:07 PM
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:46 PM
    Monday, September 12, 2011 4:07 PM
  • Than right click on your GridView in the desiger and select Show Code there you will see those boxes I wrote about.

     


    Success
    Cor
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:46 PM
    Monday, September 12, 2011 4:14 PM
  • Lord Have Mercy!  It WORKED!

    Thank you so much!

     

    Got to tell you though...sure miss the old days when you could select the object and then select the event you want to code for it...and visual studio would stub in the subroutine for you to code against...but hey, I can at least move forward now.

     

    Thanks once again!


    Joseph Everett
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:46 PM
    Monday, September 12, 2011 6:00 PM

All replies

  • Go to your code behind page and look above the code, there are two dropdowns aside from each other. Select in the left one the your GridView1, 

    Select that than you go on the same line the dropdown right and select the selectedIndexchange event.


    Success
    Cor
    • Edited by Cor Ligthert Monday, September 12, 2011 2:31 PM
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:47 PM
    Monday, September 12, 2011 2:31 PM
  • Actually, I already tried that.  What I did was delete the auto generated code event for the selected index changed.  Then I manually selected the object from the left drop down and the event on the right drop down.

     

    Same result.  Same error.

     


    Joseph Everett
    Monday, September 12, 2011 2:34 PM
  • look at your forms .designer file (select teh project and select Show All Files at the top of server explorer), and look at the definition of your controls. my guess is that everything is declared as protected withevents except for your datagrid. correct the declaration there, save, and then try to create a codebehind event as Cor suggested.
    Monday, September 12, 2011 3:12 PM
  • I see .designer files for all my datasets true enough...but a form .designer file?  I checked via Windows Explorer and there is no .designer file that I can see there either.

     

    Here's the code behind (ALL of it):

     

    Partial Class wucPatientSearch
        Inherits System.Web.UI.UserControl
     
        Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgsHandles GridView1.SelectedIndexChanged
     
        End Sub
    End Class

    Joseph Everett
    Monday, September 12, 2011 3:21 PM
  • The datagrid is bound to sql datasource that calls a stored proc.  It works like a charm.  BUT...if I attempt to select the object in the code behind...and select any event...I get that same error.

     

    I tried replicating this, so I started a new project entirely.  Slapped a data grid on a default.aspx and bound it.  Run and it works great.

     

    Try to select an event to code against....forget it.  Same error.  I even tried uninstalling and reinstalling visual studio (I have the latest build of VS 2010 Ultimate).

     

    That doesn't work either.

     

    I am literally pulling my hair out.  I've been coding in vb since vb 3.0, web since .net 1.0.  It's not like I'm inexperienced...but I've never seen anything like this before.

     

    I ready to drop back to .net 2.0...at least it works.

     


    Joseph Everett
    Monday, September 12, 2011 3:25 PM
  • There is not any sentence in that event.

    Be also aware that with a post back you have to set the datasource again of the gridview. 

    Net has no state so you have to keep your data somewhere (in a settion or a viewstate or whatever)

     


    Success
    Cor
    Monday, September 12, 2011 3:35 PM
  • I know there's no actual user code defined.  All I did was select the object in the object drop down, select the event in the event drop down...and this code was generated.....and I can't compile because of that error.

     

    However, if I remove that auto-generated code then it compiles and runs ok.  I need to code to the selected index changed event in order to take the next step...BUT....I can't.

     

    I was just showing the visual studio generated code that makes visual studio not allow the project to compile anymore.


    Joseph Everett
    Monday, September 12, 2011 3:38 PM
  • As for the datasource, yes, I'm aware.  But, thank you anyway.  My problem is very simple.  I can add a gridview control to a form.  I can bind it, I can run the code.  It all works just peachy.

     

    BUT, try to code against any event of that grid view and Visual Studio generates errors...ON CODE IT ITSELF CREATED!

     

    It's almost funny, like visual studio is at war with itself.


    Joseph Everett
    Monday, September 12, 2011 3:40 PM
  • in that case, can you clarify that you are using a Windows Forms project? if so, each form should consist of 3 files:

    Form.resx (xml document)

    Form.designer.vb (auto-gen control instatiation)

    form.vb (code-behind file)

    you should be able to see them in explorer, or if you click "Show All Files" at the top of Solution Explorer.

    here is an excerpt from the very bottom of one of my .designer.vb files:

        Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
        Friend WithEvents tpSystems As System.Windows.Forms.TabPage
        Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
        Friend WithEvents btnExit As System.Windows.Forms.Button
        Friend WithEvents SystemsAddEdit1 As Proj_Administrator.SystemsAddEdit

     

    every once in a while, I will get an error like the one you describe, and when I look at the designer.vb, the control is declared differantly from the others.

    Monday, September 12, 2011 3:48 PM
  • It's a web project.

     

    From earlier post:

    Partial Class wucPatientSearch
        Inherits System.Web.UI.UserControl
     
        Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgsHandles GridView1.SelectedIndexChanged
     
        End Sub
    End Class

    Joseph Everett
    Monday, September 12, 2011 3:50 PM
  • Your question made me try something.  I created a Windows Forms project as a test.  Slapped a DataGridView on it, bound it and ran.

     

    Works ok.

     

    Then, I did was is causing the issue with my WEB project...I doubled clicked the grid, jumped into the code behind on the selected index changed and just typed some random code.

     

    It compiles and works.  Maybe this is a WEB project issue?

     


    Joseph Everett
    Monday, September 12, 2011 3:56 PM
  • This works ok in my web user control:

    Partial Class wucPatientSearch
        Inherits System.Web.UI.UserControl
     
     
    End Class


    This does not:

    Partial Class wucPatientSearch     Inherits System.Web.UI.UserControl     Protected Sub GridView1_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgsHandles GridView1.RowUpdating     End Sub End Class

    Joseph Everett
    Monday, September 12, 2011 3:58 PM
  • Neither does this:

     

    Partial Class wucPatientSearch
        Inherits System.Web.UI.UserControl
     
     
       
        Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgsHandles GridView1.SelectedIndexChanged
     
        End Sub
    End Class

    Joseph Everett
    Monday, September 12, 2011 3:59 PM
  • Just the simple act of selecting the control...and ANY event tied to it...EVEN IF YOU DON"T PUT ANY CODE YOURSELF IN IT...causes the error.

     


    Joseph Everett
    Monday, September 12, 2011 4:00 PM
  • And that error is: "Handles clause requires a WithEvents variable defined in the containing type or one of its base types."

     

    HUH?  This is a PARTIAL CLASS.  Just the sheer act of dropping a gridview onto a web form should automatically generate that base declaration.

     

    Even if I try to manually add it visual studio then jumps back at me and says it's already there.

     

    HUH AGAIN!?

     

    Visual Studio is saying the object (GridView1) has no base WithEvents declaration....IF I select any event to code against.

     

    BUT, if say "Ok  Visual Studio...clearly you're on drugs so here's the Protected WithEvents you claim you don't have"  then Visual Studio comes back at me and say it has it!!!

     

    OMG!  Visual Studio what are you doing to me?

     


    Joseph Everett
    Monday, September 12, 2011 4:04 PM
  •  

     

    in that case, can you post the markup for that control? in ASP, controls get defined in the page markup (source view), and their markup configures how they use code-behind events.

     

    here is a sample. note OnRowCommand (another event like SelectedIndexChanged):

      <asp:GridView ID="gvUsers" runat="server" 
         AutoGenerateColumns="False" 
         GridLines="None" 
         AllowPaging="true" 
         CssClass="mGrid" 
         PagerStyle-CssClass="pgr" 
         AlternatingRowStyle-CssClass="alt"
         AutoGenerateSelectButton="true"
         AllowSorting="true"
         datakeynames="UserID"
         onrowcommand="UserGridView_RowCommand"
         >

    and in my codebehind I have (Note, there is no handles clause, the markup handles that):

    Public Sub UserGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)


    • Edited by doas777 Monday, September 12, 2011 4:07 PM
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:46 PM
    Monday, September 12, 2011 4:07 PM
  • Than right click on your GridView in the desiger and select Show Code there you will see those boxes I wrote about.

     


    Success
    Cor
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:46 PM
    Monday, September 12, 2011 4:14 PM
  • Lord Have Mercy!  It WORKED!

    Thank you so much!

     

    Got to tell you though...sure miss the old days when you could select the object and then select the event you want to code for it...and visual studio would stub in the subroutine for you to code against...but hey, I can at least move forward now.

     

    Thanks once again!


    Joseph Everett
    • Marked as answer by Jie Bao Monday, September 19, 2011 5:46 PM
    Monday, September 12, 2011 6:00 PM