Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear experts,
I am working on creating multiple gridview to generate reports based on category. each gridview has save data fields. the only different is the data. I try to search through out google... got some idea... but not working...

I hope experts can help

What I have tried:

Private dynamicGrids() As GridView

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim myControl As Control = GetPostBackControl(Me.Page)

        If (myControl IsNot Nothing) Then
            If (myControl.ClientID.ToString() = "btnAddGrid") Then
                myCount = myCount + 1
            End If
        End If
    End Sub

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)
        dynamicGrids = New GridView(myCount - 1) {}
        Dim i As Integer

        For i = 0 To myCount - 1 Step i + 1
            Dim dynGridView As New GridView
            dynGridView.ID = "Gridview" + (i + 1).ToString

            gridPlaceHolder.Controls.Add(dynGridView)
            dynamicGrids(i) = dynGridView

            PopulateGrids(dynGridView)

            Dim literalBreak As LiteralControl = New LiteralControl("<br />")
            gridPlaceHolder.Controls.Add(literalBreak)
        Next
    End Sub


    Protected Sub btnAddGrid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddGrid.Click
        ' Handled in preInit due to event sequencing.

    End Sub

    Public Shared Function GetPostBackControl(ByVal thePage As Page) As Control
        Dim myControl As Control = Nothing
        Dim ctrlName As String = thePage.Request.Params.Get("__EVENTTARGET")
        If ((ctrlName IsNot Nothing) And (ctrlName <> String.Empty)) Then
            myControl = thePage.FindControl(ctrlName)
        Else
            For Each Item As String In thePage.Request.Form
                Dim c As Control = thePage.FindControl(Item)
                If (TypeOf (c) Is System.Web.UI.WebControls.Button) Then
                    myControl = c
                End If
            Next

        End If
        Return myControl
    End Function

    Private Sub PopulateGrids(ByVal grd As GridView)
        Dim b As New BusinessRules.BusinessRules

        If grd.ID = "Gridview1" Then
            grd.DataSource = b.GetData("Select CategoryId, CategoryName from Categories")
        ElseIf grd.ID = "Gridview2" Then
            grd.DataSource = b.GetData("Select OrderID, OrderDate from Orders")
        ElseIf grd.ID = "Gridview3" Then
            grd.DataSource = b.GetData("Select productid, productname from products")
        ElseIf grd.ID = "Gridview4" Then
            grd.DataSource = b.GetData("Select customerid, Companyname from customers")
        Else
            grd.DataSource = b.GetData("Select supplierid, Companyname from Suppliers")
        End If
        grd.DataBind()
    End Sub
Posted
Updated 8-Apr-19 14:57pm
Comments
[no name] 8-Apr-19 11:09am    
So what happens with the code you wrote? Or is this a code review? Which is not a question.

1 solution

from my aspx page
place the following code

<asp:PlaceHolder ID="gridPlaceHolder" runat="server"></asp:PlaceHolder>


and the serverside codes:
Private dynamicGrids() As GridView
Private Sub getBOMComp()
      Dim ds1 As New DataSet
      Dim iGV As Integer = 0
      Try
          If SQLConnected() Then
              ds1 = getBOMCompTitle()
              If dsCheck(ds1) Then
                  dynamicGrids = New GridView(ds1.Tables(0).Rows.Count - 1) {}
                  For Each r As DataRow In ds1.Tables(0).Rows
                      Dim dynGridView As New GridView
                      iGV += 1
                      dynGridView.ID = "Gridview" + (iGV).ToString
                      setGVColumnsProperties(dynGridView, r("brdBomTtl").ToString)
                      PopulateGrids(dynGridView, r("brdBomTtl").ToString)
                      gridPlaceHolder.Controls.Add(dynGridView)
                      dynamicGrids(iGV - 1) = dynGridView
                      Dim literalBreak As LiteralControl = New LiteralControl("<br />")
                      gridPlaceHolder.Controls.Add(literalBreak)
                  Next
              End If
          End If
      Catch ex As Exception
          PromptError(ex.Message)
      End Try
  End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900