Joe SyeReadi
Joe SyeReadi

Reputation: 1

ASP.NET repeater how to get two columns (header and items) to vertical

Can you help me?

I have a table in my database like this:

January February
100.000 200.000

and I want to show that data like this using a repeater:

January 100.000
February 200.000

Please help me, so that the display in my ASP.NET frontend can appear like that; I use VB.NET.

Upvotes: 0

Views: 328

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49329

Ok, since we talking about a table of data?

Then using a repeater really not the correct type of "repeating" control.

So, a repeater for each data row would be say good for this type of repeat:

enter image description here

Or say maybe like this:

enter image description here

In other words, it not for a table like layout.

For a table like layout, I suggest using a gridview - it much better for this purpose.

So, we can drop in a gridview - I used the wizards to create this - then a delete the data source from the page. Regardless, we have this now:

    <div style="width:20%;margin-left:20px;margin-top:20px">
        
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="table table-hover">
            <Columns>
                <asp:BoundField DataField="MyMonth" HeaderText="MyMonth"  />
                <asp:BoundField DataField="Sales" HeaderText="Sales" DataFormatString="{0:C}"/>
            </Columns>

        </asp:GridView>

    </div>

So, now we need to de-normalize your VERY bad data. That data looks like a spredsheet - and it not really very good for "data" or a "data driven approach.

So, lets write this code behind to load up the data.

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 1 as NMonth, 'January'  as MyMonth, January  As Sales from tblMonth " &
       "UNION SELECT 2 as NMonth, 'Feburary' as MyMonth, Feburary As Sales from tblmonth " &
       "UNION SELECT 3 as Nmonth, 'March'    as MyMonth, March    As Sales from tblmonth " &
       "UNION SELECT 4 as Nmonth, 'April'    as MyMonth, April    As Sales from tblmonth " &
       "ORDER BY Nmonth"

    Using cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.TEST4))

        cmdSQL.Connection.Open()

        Dim rstData As New DataTable
        rstData.Load(cmdSQL.ExecuteReader)
        GridView1.DataSource = rstData
        GridView1.DataBind()

    End Using

End Sub

And now our ouput looks like this:

enter image description here

our data table looks like this:

enter image description here

So, that database needs some love and care - it should not be setup that way (with each column as a month. The database should be:

  DMonth - (the month - probably better as a date
  Sales  - (the sales for the month - say money data type)

Upvotes: 1

Related Questions