打印Data.grid表,在asp.net中的多个页面中包含许多列标题

时间:2014-11-28 06:44:39

标签: css asp.net vb.net google-chrome datagrid

我无法根据下面的链接进一步解释标题。 我在ASP.net中使用DataGrid(不是GridView)使用VB代码。

这是我在datagrid中的当前显示 click here to view picture1 ,我想这样做clck here to view picture2

如果我有多列,我的目的是将我的数据网格分割成多个数据网格。 我需要拆分它们,以便我可以很好地打印它。因为图片1中使用javascript的window.print的打印预览是它在一个dataGrid中自动适合我的多列,因此,如果我有,让我们说,20列数据,我的列的间距将是小的,用户将无法读取。像这样click here to view picture3

如果你有关于如何在浏览器中打印我的数据网格的建议,不好意思, TIA。

<asp:DataGrid Visible ="true" OnItemDataBound="Item_Bound" ID="dgSheet" 
   runat="server" BackColor="White" BorderColor="black"  BorderStyle="None" 
   BorderWidth="1px" CellSpacing="0" CellPadding="0" Width="100%" PageSize="5"
   CssClass="Narrow" ForeColor="Black">
 <EditItemStyle BackColor="#999999" />
 <FooterStyle BackColor="#2980b9" Font-Bold="True" ForeColor="White" />
 <PagerStyle BackColor="#2980b9" ForeColor="White" HorizontalAlign="Center" />
 <ItemStyle Font-Size="12px" Width="200"  ForeColor="#333333" />
 <HeaderStyle Font-Bold="true" Font-Size="12px" Width="200"  ForeColor="black" />
 <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:DataGrid>

我在数据网格中显示数据的部分代码:

   Dim o_Row As DataRow
    Dim o_AdmDates As New Collection()
    Dim s_LastAdmDate As String = ""
    Dim s_AdmDate As String = ""
    Dim o_DerivedTable As New DataTable()

    With o_DerivedTable

        .Columns.Add("TransDate")
        .Columns.Add("Medication")
        .Columns.Add("Dosage")
        .Columns.Add("TransNum")
        .Columns.Add("AdministeredDate")
        .Columns.Add("newAdmed")
    End With

    'Sort by administered dates
    Dim o_FoundRows As DataRow() = o_Dataset.Tables(0).Select("", "AdministeredDate Desc")

    'Extract distinct  administered dates
    For Each o_Row In o_FoundRows
        s_AdmDate = Format(CDate(o_Row.Item("AdministeredDate")), KC_Date_Format2)

        If s_LastAdmDate <> s_AdmDate Then
            s_LastAdmDate = s_AdmDate
            o_AdmDates.Add(s_LastAdmDate)
        End If
    Next

    'Add administred date to derived table
    Dim o_Item As String
    For Each o_Item In o_AdmDates
        o_DerivedTable.Columns.Add(o_Item)
    Next


    'Loop through the administred date
    Dim o_NewRow As DataRow
    Dim o_NextRow As DataRow
    Dim i_Ctr As Integer
    Dim x_isNewRow As Boolean = True
    Dim i_MaxRec As Integer

    i_MaxRec = o_Dataset.Tables(0).Rows.Count - 1

    For i_Ctr = 0 To i_MaxRec
        o_Row = o_Dataset.Tables(0).Rows(i_Ctr)
        If i_Ctr <> i_MaxRec Then
            o_NextRow = o_Dataset.Tables(0).Rows(i_Ctr + 1)
        End If

        If x_isNewRow Then
            o_NewRow = o_DerivedTable.NewRow()
        End If

        o_NewRow("TransDate") = o_Row("TransDate")
        o_NewRow("Medication") = o_Row("Medication")
        o_NewRow("Dosage") = o_Row("Dosage")
        o_NewRow("TransNum") = o_Row("TransNum")
        o_NewRow("AdministeredDate") = Format(CDate(o_Row("AdministeredDate")), KC_Date_Format2)
        o_NewRow("newAdmed") = o_Row("newAdmed")


        'Fill approriate result date column based on query
        For Each o_Item In o_AdmDates
            s_AdmDate = Format(CDate(o_Row("AdministeredDate")), KC_Date_Format2)
            Dim AdmTim As DateTime = DateTime.Parse(o_Row("AdministeredDate"))
            If s_AdmDate = o_Item Then
                o_NewRow(s_AdmDate) = AdmTim.ToString("hh:mm tt") + " - " + o_Row("UserID")
            End If
        Next

        If i_Ctr < i_MaxRec _
            AndAlso Not o_NextRow Is Nothing _
            AndAlso o_Row("TransDate") = o_NextRow("TransDate") _
            AndAlso o_Row("Medication") = o_NextRow("Medication") _
            AndAlso o_Row("AdministeredDate") = o_NextRow("AdministeredDate") Then
            x_isNewRow = False
        Else
            o_DerivedTable.Rows.Add(o_NewRow)
            x_isNewRow = True
        End If

    Next
    dgSheetPrint.DataSource = o_DerivedTable
    dgSheetPrint.DataBind()

3 个答案:

答案 0 :(得分:1)

首先,我想说你不打印datagrid,而是生成的html。 因此,您需要在客户端做一些事情。既然你已经写了,你需要打破表然后打印我附加一个html页面,你可以从中复制代码并将其更改为使用datagrid生成的html,你需要至少更改datagrid的id。

代码如下:

[因为你没有提供生成的html我已经制作了类似的表格]

    

<table id="previousOrderExports" class="chromeTheme">
    <tr>
        <td class="bold">Export ID</td>
        <td class="bold">Request Type</td>
        <td class="bold">Timeframe</td>
        <td class="bold">Search Filter</td>
        <td class="bold">Search Term</td>
        <td class="bold">Requested on</td>
        <td class="bold">Processed on</td>
        <td class="bold">Export Status</td>
        <td class="bold">Download</td>
    </tr>
    <tr>
        <td>143306</td>
        <td>manual</td>
        <td>11/8/14 - 12/8/14</td>
        <td>Timespan</td>
        <td></td>
        <td>12/08/14 03:37:00 AM PST</td>
        <td>12/08/14 03:37:08 AM PST</td>
        <td>Done</td>
        <td><a href="fghhsd.dsfd/sfre/143306">Download</a></td>
    </tr>
    <tr>
        <td>142873</td>
        <td>auto</td>
        <td>12/7/14 - 12/8/14</td>
        <td>Timespan</td>
        <td></td>
        <td>12/08/14 01:01:10 AM PST</td>
        <td>12/08/14 01:03:00 AM PST</td>
        <td>Done</td>
        <td><a href="fghhsd.dsfd/sfre/142873">Download</a></td>
    </tr>
    <tr>
        <td>142766</td>
        <td>auto</td>
        <td>12/7/14 - 12/8/14</td>
        <td>Timespan</td>
        <td></td>
        <td>12/07/14 11:01:03 PM PST</td>
        <td>12/07/14 11:02:04 PM PST</td>
        <td>Done</td>
        <td><a href="fghhsd.dsfd/sfre/142766">Download</a></td>
    </tr>
    <tr>
        <td>142752</td>
        <td>auto</td>
        <td>12/7/14 - 12/8/14</td>
        <td>Timespan</td>
        <td></td>
        <td>12/07/14 10:01:03 PM PST</td>
        <td>12/07/14 10:02:05 PM PST</td>
        <td>Done</td>
        <td><a href="fghhsd.dsfd/sfre/142752">Download</a></td>
    </tr>
    <tr>
        <td>142738</td>
        <td>auto</td>
        <td>12/7/14 - 12/8/14</td>
        <td>Timespan</td>
        <td></td>
        <td>12/07/14 09:01:03 PM PST</td>
        <td>12/07/14 09:02:07 PM PST</td>
        <td>Done</td>
        <td><a href="fghhsd.dsfd/sfre/142738">Download</a></td>
    </tr>
    <tr>
        <td>142723</td>
        <td>auto</td>
        <td>12/7/14 - 12/8/14</td>
        <td>Timespan</td>
        <td></td>
        <td>12/07/14 08:01:02 PM PST</td>
        <td>12/07/14 08:02:04 PM PST</td>
        <td>Done</td>
        <td><a href="fghhsd.dsfd/sfre/142723">Download</a></td>
    </tr>
    <tr>
        <td>142709</td>
        <td>auto</td>
        <td>12/7/14 - 12/8/14</td>
        <td>Timespan</td>
        <td></td>
        <td>12/07/14 07:01:03 PM PST</td>
        <td>12/07/14 07:02:04 PM PST</td>
        <td>Done</td>
        <td><a href="fghhsd.dsfd/sfre/142709">Download</a></td>
    </tr>
</table>
<input id="Button1" type="button" value="Print" onclick="print_breakup_table()" />
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    function print_breakup_table()
    {
        var tableid = "previousOrderExports"; //id of table in this case cliend side id of your datagrid
        var fixedcols = 2;// no. of columns 0 index based that will be repeated on every page
        var eachpagecols = 3; // no. of columns that will get printed on each page
        var colCount = 0;
        $('#' + tableid + ' tr:nth-child(1) td').each(function () {
            if ($(this).attr('colspan')) {
                colCount += +$(this).attr('colspan');
            } else {
                colCount++;
            }
        });
        colCount--; //get 0 based columncount
        var col_left_after_fixed_col = colCount - fixedcols;
        var pagerequired = col_left_after_fixed_col / eachpagecols;
        if ((col_left_after_fixed_col % eachpagecols)>0) {
            pagerequired++;
        }
        for (var i = 0; i < pagerequired; i++) {
            var html = "<table>";
            var startcols = fixedcols + (i * eachpagecols);
            var endcols = startcols + eachpagecols;
            //add fixed cols
            $('#' + tableid + ' tr').each(function () {
                html += "<tr>";
                $(this).children('td').each(function (index, object) {
                    if (index <= fixedcols) {
                        html += "<td>" + $(this).html() + "</td>";
                    } else {
                        if (index <= endcols && index >= startcols) {
                            html += "<td>" + $(this).html() + "</td>";
                        }
                    }
                });
                html += "</tr>";
            });



            html += "</table>";
            myWindow = window.open('', '', 'width=800,height=600');
            myWindow.document.write(html);


            myWindow.document.close();


            myWindow.focus();
            myWindow.print();
        }

    }
</script>

答案 1 :(得分:0)

如果您仍想使用window.print:不要依赖内联样式,而是创建2个单独的样式表:media =&#34; screen&#34; &安培;媒体=&#34;打印&#34;

使用报告引擎完全控制结果,例如Crystal Report,Telerik Reporting等

答案 2 :(得分:0)

您需要专门编写一个包含所需表格的视图。然后你可以添加一个按钮来打印它

 <a class="miniprint" href="/url/to/print/view">Print</a>

使用Javascript:

$(document).on('click', '.miniprint', function (event) {
    event.preventDefault();
    var self = $(this);
    var url = self.attr("href");
    $("#printjob").remove();
    $('<iframe id="printjob" src="' + url + '">')
    .load(function () {
        this.focus();
        document.getElementById("printjob").contentWindow.print();
    })
    .css({ position: "absolute", left: "-10000px", top: "0px" })
    .appendTo(document.body);
});

另外,我建议告诉Google Chrome使用

以横向打印
@media print {
  @page {size: landscape}
}