Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Gridview - Matrix format

Posted on 2011-03-03
3
Medium Priority
?
1,370 Views
Last Modified: 2012-08-14
I want to display the data in the following format in a gridview whenever a user is selected.

              Username1     Username2     Password
IPAD         XXXXX11        XXXXX21         XXXXXp1
PC1          XXXXX12        XXXXX22         XXXXXp2
PC2          XXXXX13        XXXXX23         XXXXXp3    
PC3          XXXXX14        XXXXX24         XXXXXp4  

I have two tables 1) tblMachines 2)tblPasswords
1) tblMachine -->
     MachineId,
     MachineName
2) tblPasswords -->
     PassId
     UserId
     MachineId
     Username1
     Username2
     Password

Would someone help me. Thanks.
0
Comment
Question by:cimscims
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35036335
That looks like a pivot/cross-tab, but you didn't indicate the database that you are using.  That could either be the job for a query, or you could do it in code also.
0
 

Author Comment

by:cimscims
ID: 35036981
I am using SQL Server 2000. Would you please help me out. Thanks.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 35070827
SQL Server 2000 does not support the PIVOT and UNPIVOT keywords, since they were added in 2005.  That would mean that you need to do the cross-tab pivoting in code.

There are some people who have the opinion that cross-tabs in the presentation layer are better performers.

.NET CrossTabs versus SQL Server CrossTabs
http://weblogs.sqlteam.com/jeffs/archive/2005/05/12/5127.aspx

Example:

CrossTab Performance Testing
http://weblogs.sqlteam.com/jeffs/articles/5124.aspx
Namespace CSharpConsole

Imports System.Data.SqlClient
Imports System.Data

    Class CrossTabTesting
        <STAThread()> _
        Private Shared Sub Main(ByVal args As String())
            Dim conn As SqlConnection
            Dim s As String
            Dim i As Integer

            conn = New SqlConnection("Server=(local);Database=Northwind;uid=xx;pwd=xx")
            conn.Open()

            Console.WriteLine("Beginning performance test for crosstab techniques." & vbLf)
            Console.WriteLine("Please enter the # of iterations to perform:")
            s = Console.ReadLine()
            Try
                i = Convert.ToInt32(s)
                Console.WriteLine(vbLf & "Testing dynamic SQL crosstab procedure....")
                Console.WriteLine("-- Result: {0} seconds." & vbLf, TestCrossTabDynamic(conn, i))
                Console.WriteLine("Testing static SQL crosstab procedure....")
                Console.WriteLine("-- Result: {0} seconds." & vbLf, TestCrossTabStatic(conn, i))
                Console.WriteLine("Testing C# crosstab transformation....")
                Console.WriteLine("-- Result: {0} seconds." & vbLf, TestPivotFunction(conn, i))
                Console.WriteLine(vbLf & "Testing complete.")
            Catch ex As Exception
                Console.WriteLine(vbLf & "Error: " & ex.Message)
                Console.WriteLine("Program aborted...")
            Finally
                conn.Close()
                Console.WriteLine(vbLf & "Press ENTER to quit.")
                Console.ReadLine()
            End Try
        End Sub

        Private Shared Function TestPivotFunction(ByVal conn As SqlConnection, ByVal iterations As Integer) As Integer
            Dim dt As DataTable

            Dim SQL As [String] = "select * from PivotTest order by CustomerID"
            Dim com As New SqlCommand(SQL, conn)

            Dim t As DateTime = DateTime.Now

            While System.Math.Max(System.Threading.Interlocked.Decrement(iterations), iterations + 1) > 0
                dt = Pivot(com.ExecuteReader(), "CustomerID", "ProductName", "Qty")
            End While

            Return DateTime.Now.Subtract(t).Seconds
        End Function

        Private Shared Function TestCrossTabDynamic(ByVal conn As SqlConnection, ByVal iterations As Integer) As Integer
            Dim da As SqlDataAdapter
            Dim dt As DataTable

            Dim SQL As String = "exec CrossTab2 'select * from PivotTest','ProductName','SUM(Qty)[]','CustomerID, CompanyName'"
            Dim com As New SqlCommand(SQL, conn)

            Dim t As DateTime = DateTime.Now

            While System.Math.Max(System.Threading.Interlocked.Decrement(iterations), iterations + 1) > 0
                dt = New DataTable()
                da = New SqlDataAdapter(com)
                da.Fill(dt)
            End While

            Return DateTime.Now.Subtract(t).Seconds
        End Function

        Private Shared Function TestCrossTabStatic(ByVal conn As SqlConnection, ByVal iterations As Integer) As Integer
            Dim da As SqlDataAdapter
            Dim dt As DataTable

            ' set the parameter so we get a SQL string only back that we can execute directly:
            Dim SQL As String = "exec CrossTab2 'select * from PivotTest','ProductName','SUM(Qty)[]','CustomerID, CompanyName',Null,1"

            Dim com As New SqlCommand(SQL, conn)

            SQL = com.ExecuteScalar().ToString()

            ' and set up the new SQL command, already written out and optimized and ready to go:
            com = New SqlCommand(SQL, conn)

            Dim t As DateTime = DateTime.Now

            While System.Math.Max(System.Threading.Interlocked.Decrement(iterations), iterations + 1) > 0
                dt = New DataTable()
                da = New SqlDataAdapter(com)
                da.Fill(dt)
            End While

            Return DateTime.Now.Subtract(t).Seconds
        End Function

        Public Shared Function Pivot(ByVal dataValues As IDataReader, ByVal keyColumn As String, ByVal pivotNameColumn As String, ByVal pivotValueColumn As String) As DataTable
            Dim tmp As New DataTable()
            Dim r As DataRow
            Dim LastKey As String = "//dummy//"
            Dim i As Integer, pValIndex As Integer, pNameIndex As Integer
            Dim s As String
            Dim FirstRow As Boolean = True

            ' Add non-pivot columns to the data table:

            pValIndex = dataValues.GetOrdinal(pivotValueColumn)
            pNameIndex = dataValues.GetOrdinal(pivotNameColumn)

            For i = 0 To dataValues.FieldCount - 1
                If i <> pValIndex AndAlso i <> pNameIndex Then
                    tmp.Columns.Add(dataValues.GetName(i), dataValues.GetFieldType(i))
                End If
            Next

            r = tmp.NewRow()

            ' now, fill up the table with the data:
            While dataValues.Read()
                ' see if we need to start a new row
                If dataValues(keyColumn).ToString() <> LastKey Then
                    ' if this isn't the very first row, we need to add the last one to the table
                    If Not FirstRow Then
                        tmp.Rows.Add(r)
                    End If
                    r = tmp.NewRow()
                    FirstRow = False
                    ' Add all non-pivot column values to the new row:
                    For i = 0 To dataValues.FieldCount - 3
                        r(i) = dataValues(tmp.Columns(i).ColumnName)
                    Next
                    LastKey = dataValues(keyColumn).ToString()
                End If
                ' assign the pivot values to the proper column; add new columns if needed:
                s = dataValues(pNameIndex).ToString()
                If Not tmp.Columns.Contains(s) Then
                    tmp.Columns.Add(s, dataValues.GetFieldType(pValIndex))
                End If
                r(s) = dataValues(pValIndex)
            End While

            ' add that final row to the datatable:
            tmp.Rows.Add(r)

            ' Close the DataReader
            dataValues.Close()

            ' and that's it!
            Return tmp
        End Function
    End Class
End Namespace

Open in new window

0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question