Creating a Datagrid Class in classic ASP

More Analysis of the Class

Now onto the meat and veg, i.e. the Methods, procedures, for your class. These add the actual functionality to the class:-

  Public Sub AddColumn(strColName)
      If intColCnt = 0 then
          pOutPut = "<table width='100%' border=1 cellpadding=0 cellspacing=0>" & vbcrlf
          pOutPut = pOutPut & "<tr>" & vbcrlf
      End If
      pOutPut = pOutPut & "<td><strong>" & strColName & "</strong></td>" & vbcrlf
      intColCnt = intColCnt + 1
  End Sub


If we choose to specify our own column names then we call this method to add a column name. It just adds a new cell to our grid(table) for each column and this is where the column count comes in. We only want to start the table and the row if it's at 0. Every other time we want to just add the cell code. Once thats done, or not if you use AutoColumns, it's on to the Binding of the grid:-

Public Sub Bind
      pConn.Open pConnStr
      Set pRec = pConn.Execute(pSqlStr)
      If pAutoColumns = True then
          'assign column names from returned recordset
          pOutPut = "<table width='100%' border=1 cellpadding=0 cellspacing=0>" & vbcrlf
          pOutPut = pOutPut & "<tr>" & vbcrlf
          Redim pColNames(pRec.Fields.Count)
          For x = 0 to pRec.Fields.Count - 1
              pOutPut = pOutPut & "<td>" & pRec.Fields(x).Name & "</td>" & vbcrlf
          Next
      End If
      pOutPut = pOutPut & "</tr>" & vbcrlf
      pArray = pRec.GetRows
      For x = 0 to UBound(pArray, 2)
          pOutPut = pOutPut & "<tr>" & vbcrlf
          For y = 0 to UBound(pArray, 1)
  pOutPut = pOutPut & "<td>" & pArray(y, x) & "</td>" & vbcrlf
          Next
          pOutPut = pOutPut & "</tr>" & vbcrlf
      Next
      pOutPut = pOutPut & "</table>" & vbcrlf
      Response.Write pOutPut
  End Sub

Basically we open our recordset, if AutoColums=true then we get the fields names and create cells for them or we go with the custom column names, then we use the code already discussed to cycle through our array of values. All of this is being concatenated to an output string which is finally written to the browser at the end. Now lets use the class in an asp page.

You might also like...

Comments

About the author

Brian O'Connell Ireland

Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web appli...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay