Horizontal DataGrid?

Tom Chesley

Member
Joined
Jun 18, 2006
Messages
12
Programming Experience
1-3
I currently have a sql 2000 database with a list of users and I can display a datagrid on my web with all of the users, the problem is I now want to display like 4 columns of the users so i dont have to scroll down on my web page

Say for instance I have 5 users
user1
user2
user3
user4
user5

I want to see
user1 user2 user3 user4
user5

Is that possible?

my current vb code is
VB.NET:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim cmd As String = "SELECT UserName "

        'set Focus on textbox
        Page.RegisterStartupScript("SetFocus", _
        "<script>document.getElementById('" & txtBadGuy.ClientID & _
        "').focus();</script>")

        cmd &= "FROM MyUsers ORDER BY UserName"
        DO_Cmd(cmd)
        dg.Caption = "Users"
        dg.DataBind()
    End Sub
    Private Sub DO_Cmd(ByVal cmd)
        Dim Count As Integer
        Dim RowsCnt As Integer
        Dim Extras As Integer
        'create SQL command
        Dim selCmd As New SqlClient.SqlCommand(cmd)
        'create data adapter
        Dim PTAdapter As New SqlClient.SqlDataAdapter(selCmd)
        'set the connector to the adapter
        PTAdapter.SelectCommand.Connection = con
        'create dataset
        Dim ds As New DataSet
        Try
            'fill data adapter
            PTAdapter.Fill(ds, "UserName")

            'bind the datagrid
            dg.DataSource = ds
            dg.DataMember = "UserName"
        Catch ex As Exception
            lblError.Text = ex.Message
        Finally
            con.Close()
        End Try
        Count = ds.Tables(0).Rows.Count
        RowsCnt = Count / 4
        Extras = Count - RowsCnt * 4
        lblCnt.Text = "We have " & Count & " so far"
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        lblRammer.Visible = True
        txtBadGuy.Visible = True
        btnSubmit.Visible = True
    End Sub
    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        Dim UpdateCMD As SqlClient.SqlCommand
        Dim UpdatePT As String = "INSERT INTO MyUsers VALUES ("
        UpdatePT &= "'" + txtUser.Text + "')"

        UpdateCMD = New SqlClient.SqlCommand(UpdatePT, con)
        Try
            con.Open()
            UpdateCMD.ExecuteNonQuery()
        Catch ex As Exception
            lblError.Text = ex.Message
        Finally
            con.Close()
        End Try
        Response.Redirect("http://localhost/PT/PT.aspx")
    End Sub

    Private Sub dg_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles dg.DataBinding
        'Stop
    End Sub

    Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated
        'Dim test As String
        'test = dg.Items.Item(1).DataItem
        'Stop
    End Sub
 
I was able to do what I wanted.
It might not be the cleanest way to do it but functional for what I wanted.

I made my datagrid invisible.
I added an String array to hold the data
I added a Table and while filling the datagrid I filled the string aray with the results.

I removed the add users section in this page and made separate page to add them.

Here is how I did it with VB
VB.NET:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            Dim cmd As String = "SELECT UserName "
            cmd &= "FROM MyUsers ORDER BY Username"
            DO_Cmd(cmd)
            dg.Caption = "Users"
            dg.DataBind()

            'fill my table with the results 
            'instead of showing the datagrid
            Do_Table()
        End If
    End Sub
    Private Sub DO_Cmd(ByVal cmd)
        'Dim myGridItem As DataGridItem
        Dim Count As Integer
        Dim Extras As Integer
        'create SQL command
        Dim selCmd As New SqlClient.SqlCommand(cmd)
        'create data adapter
        Dim PTAdapter As New SqlClient.SqlDataAdapter(selCmd)
        'set the connector to the adapter
        PTAdapter.SelectCommand.Connection = con
        'create dataset
        Dim ds As New DataSet
        Try
            'fill data adapter
            PTAdapter.Fill(ds, "UserName")

            'bind the datagrid
            dg.DataSource = ds
            dg.DataMember = "UserName"

        Catch ex As Exception
            lblError.Text = ex.Message
        Finally
            con.Close()
        End Try
        Count = ds.Tables(0).Rows.Count
        RowsCnt = Count / 4
        Extras = Count - RowsCnt * 4
        lblCnt.Text = "We have " & Count & " so far"
    End Sub

    Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated
        If DG_Header <> 0 Then
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Or e.Item.ItemType = ListItemType.EditItem Then
                User_Array(RowCtr, CellCtr) = CType(e.Item.DataItem, DataRowView).Row.ItemArray(0).ToString()
                If User_Array(0, 0) <> "" Then
                    CellCtr = CellCtr + 1
                End If
                If CellCtr > 3 Then
                    CellCtr = 0
                    RowCtr = RowCtr + 1
                End If
            End If
        End If
        DG_Header += 1
    End Sub
    Sub Do_Table()
        ' Create a TableItemStyle object that can be
        ' set as the default style for all cells
        ' in the table.
        Dim tableStyle As New TableItemStyle
        tableStyle.HorizontalAlign = HorizontalAlign.Center
        tableStyle.VerticalAlign = VerticalAlign.Middle
        tableStyle.Width = Unit.Pixel(100)
        ' Create more rows for the table.
        Dim i As Integer
        For i = 0 To RowsCnt + 1
            Dim tempRow As New TableRow
            Dim j As Integer
            For j = 0 To 3
                Dim tempCell As New TableCell
                tempCell.Text = User_Array(i, j)
                tempRow.Cells.Add(tempCell)
            Next j
            Table1.Rows.Add(tempRow)
        Next i

        ' Apply the TableItemStyle to all rows in the table.
        Dim r As TableRow
        For Each r In Table1.Rows
            Dim c As TableCell
            For Each c In r.Cells
                c.ApplyStyle(tableStyle)
            Next c
        Next r

        ' Create a header for the table.
        Dim header As New TableHeaderCell
        header.RowSpan = 1
        header.ColumnSpan = 4
        header.Text = "Table of Rammers"
        header.Font.Bold = True
        header.BackColor = Color.CornflowerBlue
        header.HorizontalAlign = HorizontalAlign.Center
        header.VerticalAlign = VerticalAlign.Middle

        ' Add the header to a new row.
        Dim headerRow As New TableRow
        headerRow.Cells.Add(header)

        ' Add the header row to the table.
        Table1.Rows.AddAt(0, headerRow)
    End Sub
 
Back
Top