How to create multiple gridview dynamically from code

Viewed 72

I want to have multiple gridviews in a panel. and the number of gridviews are not fixed..

So basically there should be no code in the .aspx page as i have to create the gridview in codebehind.

And for each gridview, the headerstyle.backcolor should be different (this is based on the number of gridviews)

Please help.

1 Answers

So basically there should be no code in the .aspx page as i have to create the gridview in codebehind.

says who, and why???

I mean, I might want to display some rows of data, might be 1 row of data, or 20 rows of data. Would logic THEN suggest that no markup should be on the page because I don't know ahead of time how many rows to display? (of course not - utter rubbish to think this way).

So, say this:

I want to place a text box on a form, but I don't know ahead of time if I going to have 1 or 20 text box - so I have to write the code behind to inject the textbox? Nope, once again utter rubbish to suggest or even think that way!

If you need to repeat some controls, take a guess at what control you should use to do that for you???

Why of course a repeater!!!

Same goes for wanting 1 or 20 gridviews. Use a repeater - that VERY name REPEATer in that control should give you a hint and idea as to what that control is for!!!

So, obvisouly the "thing" we are going to repeat for "N" times must have some type of data source. Don't know what that data source is, but for this post, lets just make it up.

Say a table called hotels, and for each hotel, we will want to display the people booked. So, for each hotel, we want to repeat the grid (to show the people booked in that hotel).

So, we drop in a repeater, and inside we drop in a grid view. Say like this:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>

        <asp:HiddenField ID="HotelID" runat="server" Value='<%# Eval("ID") %>' />
        <div style="border:solid 2px;width:40%;padding:8px">

            <h3>Hotel Information for <%# Eval("HotelName") %> </h3>
            <h5>Booked people</h5>
            <asp:GridView ID="GridView1" runat="server" cssClass="table" width="50%" ></asp:GridView>

        </div>
    </ItemTemplate>
</asp:Repeater>

So, VERY little markup (and no boatloads of code to create world poverty either!!!).

So, now our code to load above

We have this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadGrid()
    End If

End Sub


Sub LoadGrid()
    Dim strSQL As String

    strSQL = "SELECT * FROM tblHotels ORDER BY HotelName"

    Repeater1.DataSource = MyRst(strSQL)
    Repeater1.DataBind()

End Sub

now, for each repeater "data" event, we fill out the GV. Could be DIFFERENT data source - whatever you want to shove into that GV.

And we want to color roatate - say 3 colors over and over.

So, this simple code:

Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound

    ' for each repated item and gridview, fill out GV
    If e.Item.ItemType = ListItemType.Item Or
        e.Item.ItemType = ListItemType.AlternatingItem Then

        Dim HotelPK As Integer =
            CType(e.Item.FindControl("HotelID"), HiddenField).Value

        ' load up GV
        Dim MyGV As GridView = e.Item.FindControl("GridView1")
        Dim strSQL As String =
            "SELECT FirstName, LastName, City FROM People WHERE Hotel_ID = " & HotelPK

        MyGV.DataSource = MyRst(strSQL)
        MyGV.DataBind()

        ' now set back ground color of the gv (rotate)
        Dim sColorRotate() As String = {"aliceblue", "tan", "salmon"}
        Dim ixColor As Integer = e.Item.ItemIndex Mod 3
        MyGV.Style.Add("background-color", sColorRotate(ixColor))

    End If

End Sub

And last but not least, our "helper" MyRst function. This:

Public Function MyRst(strSQL As String) As DataTable

    Dim rstData As New DataTable

    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
        End Using
    End Using
    Return rstData

End Function

And the output result is now this:

enter image description here

So, really, in fact your case VERY much suggests that to repeat some GridViews over and over, then use a repeater to repeat that for you.

the fact that I could write out and get a working example for a SO post, and do so in LESS time then it took me to write out this post?

A repeater not only a great idea, but is quite much the least efforts and code required for this - so much so that I was as noted able to post a working code example in a simple question and answer forum in a matter of minutes of my time.

If I had to start writing code to inject HTML? I would still be writing code and not even yet have produced a answer to your question!!!

So you don't' HAVE to inject and write code to inject html. I suppose you "could", but you don't HAVE to, nor with above types of solutions? As a result, you probably would be best avoid writing a whole boatload of looping code and even a larger mess of code to inject such html into the web page.

Related