简体   繁体   中英

How to retrieve and arrange data from SQL Server 2008 R2

The data base I'm working on saves each entry in a row like this...

| PK (Jobnumber)|ReportStatus | ECD | InspectionStatus | ECD | QualificationStatus | ECD |ect..

However in the Winforms application my customer wants to display the data in a column like this...

(PK)Job Number

_________________________
Report Status     | ECD |
_________________________
Inspection Status | ECD |
_________________________
Qual. Status      | ECD |
_________________________
etc...

I can store the data easy enough, it's just one row.

My question is how to retrieve the data and re-arrange it in a datagridview like the above layout.

EDIT: when I create the datagridview I 'Tag' each cell with a unique name that will match the database header name. then I for loop thru the data.

SDR = cmd.ExecuteReader(CommandBehavior.SingleResult)
    While SDR.Read
        If SDR.HasRows Then
            For i As Integer = 0 To SDR.FieldCount - 1

                If SDR.GetName(i).Contains("ECD") Then
                    clm = "DGVC_PS_ECD"
                    Dim dt As Date = SDR.GetSqlValue(i).ToString
                    If Not dt = "Null" Then Me.DGV_PS.Rows(GetRowIndex(SDR.GetName(i))).Cells(clm).Value = dt.ToShortDateString
                ElseIf SDR.GetName(i).Contains("JobNumber") Then
                    'do nothing
                ElseIf SDR.GetName(i).Contains("Comment") Then
                    Me.TB_PS_Comment.Text = SDR.GetSqlValue(i).ToString
                ElseIf SDR.GetName(i).Contains("Status") Then
                    clm = "DGVC_PS_Status"
                    Dim sts As String = SDR.GetSqlValue(i).ToString
                    If Not sts = "Null" Then Me.DGV_PS.Rows(GetRowIndex(SDR.GetName(i))).Cells(clm).Value = sts
                End If
            Next
        End If
    End While

The GetRowIndex is a integer function that loops through the datagriview to find the rowindex.

Sorry this is my first question here on StackOverFlow.

I would use apply :

select v.*
from t cross apply
     ( ('Report Status', ReportStatus),
       ('Inspection Status', Inspection Status),
       ('Qualification Status', QualificationStatus)
     ) v(which, val);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM