I have a problem to display a data from the database. I have 6 columns(EmpName, EmpID, AdminCode, Practice, Hours, FTE) in my table and I want to display that data as below
blankArea blankArea blankarea P1(PracticeName) P2 P3 EmpName EmpID AdminCode Hours FTE Hours FTE Hours FTE
A 1 Insurance 0.14 0.03 0.00 0.00 0.00 0.00 A 1 AllDocs 0.19 0.04 0.00 0.00 0.00 0.00 B 2 Insurance 0.52 0.11 1.18 0.25 0.00 0.00 B 2 Payments 1.18 0.35 0.00 0.00 0.00 0.00 C 3 Payments 1.31 0.00 0.00 0.00 0.00 0.00
It means all detai开发者_Go百科ls are in my table practice name is also in table and I use sql server 2005. How can I display my data as above format. Practice name is display horizontally and two columns are generated automatically and fill those columns with Hours and FTE column data which is exist in database. Is repeater or datalist suitable for this. If yes then how. Have you any code regarding this. Please help.
Thanks in advance
I would use a GridView and it's RowCreated Event to generate the extra row in the header:
ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated" ></asp:GridView>
Codebehind(VB.Net):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'fake data
Dim tbl As New DataTable
tbl.Columns.Add(New DataColumn("EmpName", GetType(String)))
tbl.Columns.Add(New DataColumn("EmpID", GetType(String)))
tbl.Columns.Add(New DataColumn("AdminCode", GetType(String)))
tbl.Columns.Add(New DataColumn("P1Hours", GetType(String)))
tbl.Columns.Add(New DataColumn("P1FTE", GetType(String)))
tbl.Columns.Add(New DataColumn("P2Hours", GetType(String)))
tbl.Columns.Add(New DataColumn("P2FTE", GetType(String)))
tbl.Columns.Add(New DataColumn("P3Hours", GetType(String)))
tbl.Columns.Add(New DataColumn("P3FTE", GetType(String)))
For i As Int32 = 1 To 100
Dim newRow = tbl.NewRow
newRow("EmpName") = "EmpName" & i
newRow("EmpID") = "EmpID" & i
newRow("AdminCode") = "AdminCode" & i
newRow("P1Hours") = "P1Hours" & i
newRow("P1FTE") = "P1FTE" & i
newRow("P2Hours") = "P2Hours" & i
newRow("P2FTE") = "P2FTE" & i
newRow("P3Hours") = "P3Hours" & i
newRow("P3FTE") = "P3FTE" & i
tbl.Rows.Add(newRow)
Next
Me.GridView1.DataSource = tbl
Me.GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
'Build own custom header.
Dim grid As GridView = DirectCast(sender, GridView)
Dim newHeaderRow As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
Dim oTableCell As New TableCell()
'Add empty cell
newHeaderRow.Cells.Add(oTableCell)
'Add empty cell
oTableCell = New TableCell()
newHeaderRow.Cells.Add(oTableCell)
'Add empty cell
oTableCell = New TableCell()
newHeaderRow.Cells.Add(oTableCell)
'Add P1(PracticeName)-Cell
oTableCell = New TableCell()
oTableCell.Text = "P1(PracticeName)"
oTableCell.ColumnSpan = 2
oTableCell.Font.Bold = True
oTableCell.HorizontalAlign = HorizontalAlign.Center
newHeaderRow.Cells.Add(oTableCell)
'Add P2-Cell
oTableCell = New TableCell()
oTableCell.Text = "P2"
oTableCell.ColumnSpan = 2
oTableCell.Font.Bold = True
oTableCell.HorizontalAlign = HorizontalAlign.Center
newHeaderRow.Cells.Add(oTableCell)
'Add P3-Cell
oTableCell = New TableCell()
oTableCell.Text = "P3"
oTableCell.ColumnSpan = 2
oTableCell.Font.Bold = True
oTableCell.HorizontalAlign = HorizontalAlign.Center
newHeaderRow.Cells.Add(oTableCell)
grid.Controls(0).Controls.AddAt(0, newHeaderRow)
End If
End Sub
If you need C#, you can convert it here.
This is the result:
The first thing that pops up in my mind is to make:
- One global Gridview with: EmpName, EmpID, AdminCode, Practices
- One repeater displayed in the Practices itemtemplate of the global gridview.
- Another gridview with the details of one practices, this gridview will be in the repeater and therefor repeated for each and every practice.
This is what I should do, maybe there is an easier way not sure. Hope this will put you on track.
精彩评论