locked
GridView in WebPart RRS feed

  • Question

  •  

    Hi,

    I have a button and gridview in a WebPart.
    when i click the button i want to bind the gridview, i have done that. But i cannot see the gridview on the Site when i click the button.

    Here is the code...

    SPGridView grid;
    CreateChildControls()
    {
    Button btn = new Button();
    btn.Text = "Click";
    btn.Click += new EventHandler(btn_Click);
    grid = new SPGridView();
    grid.GridLines = GridLines.Both;
    grid.Autogeneratecolumns = false;

    this.Controls.Add(btn);
    this.Controls.Add(grid);
    }

    void btn_Click(object sender, EventArgs e)
    {
    grid = new SPGridView();
    grid.Autogeneratecolumns = false;
    grid.DataSource = dt;
    grid.DataBind();
    this.Controls.Add(grid);
    }



    Wednesday, September 24, 2008 12:47 PM

Answers

  • You shouldn't re-add the Grid in your click event.

    To be safe you should also call EnsureChildControls(); at the beginning of the btn_Click method.

    Does (dt) have any data in it?

    Posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, September 25, 2008 12:52 PM

All replies

  • Is that your actual code, or did you modify it to put it in this forumn? The reason I ask is because I would expect that code to fail. You are referencing "grid" in your button click event, but grid is instantiated int the "CreateChildControls" method and is not a local variable. Thus, "grid" should be null in your button click event.
    Have you tried debugging your code to see what is happening? To debug in SharePoint just set some breakpoints and attach to the w3wp.exe process that is running SharePoint. Then you can hit your breakpoints and really see what is happening. Here is an article on how to do it: http://it.toolbox.com/blogs/programming-life/debugging-your-sharepoint-web-part-11176
    Wednesday, September 24, 2008 12:55 PM
  •   Sorry Greg i had done some mistakes while posting. Now I have modified my previous post.

    I have tried by putting breakpoints and debug, i can see the data in grid.DataSource but the grid is not visible in the site.
    Thursday, September 25, 2008 6:34 AM
  • You shouldn't re-add the Grid in your click event.

    To be safe you should also call EnsureChildControls(); at the beginning of the btn_Click method.

    Does (dt) have any data in it?

    Posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, September 25, 2008 12:52 PM
  • Hai Friend,

                    I have also this same problem just a week ago, now which was solved, I use UserControl to my Webpart. Create a User Control as GridView. Then bind the items to the Gridview. if you need more information please contact me as in my id anilchitatil@gmail.com
     
    • Proposed as answer by AnilChitatil Wednesday, October 1, 2008 7:47 AM
    Wednesday, October 1, 2008 7:47 AM