在asp.net中打印多个页面的许多列标题的Datagrid表 [英] Print Datagrid table with many column headers accross multiple pages in asp.net

查看:222
本文介绍了在asp.net中打印多个页面的许多列标题的Datagrid表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在标题上进一步解释以下链接。
我使用DataGrid(不是GridView),在ASP.net使用VB代码。



这是我当前显示在我的datagrid
点击这里查看图片1
,我想像这样 clck here to view picture2



我的目的是将我的datagrid拆分成多个datagrid列。
我需要拆分他们,以便我可以打印它谷歌Chrome浏览器。因为图片1中使用javascript的window.print的打印预览是它在一个dataGrid中自动适合我的多列,因此,如果我有,20列的数据,我的列的间距将很小,它不会对用户可读。像这样点击这里查看图片3



如果您有建议如何在浏览器中打印我的datagrid,不好意思,
TIA。

 < asp:DataGrid可见=trueOnStyle =None
BorderWidth =1pxCellSpacing =true 0CellPadding =0Width =100%PageSize =5
CssClass =NarrowForeColor =Black>
< EditItemStyle BackColor =#999999/>
< FooterStyle BackColor =#2980b9Font-Bold =TrueForeColor =White/>
< PagerStyle BackColor =#2980b9ForeColor =WhiteHorizo​​ntalAlign =Center/>
< ItemStyle Font-Size =12pxWidth =200ForeColor =#333333/>
< HeaderStyle Font-Bold =trueFont-Size =12pxWidth =200ForeColor =black/>
< SelectedItemStyle BackColor =#E2DED6Font-Bold =TrueForeColor =#333333/>
< / asp:DataGrid>

我在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(AdministidDate)
.Columns.Add newAdmed)
结束于

按照管理日期排序
Dim o_FoundRows As DataRow()= o_Dataset.Tables(0).Select(,AdministeredDate Desc )

'提取不同的管理日期
对于每个o_Row在o_FoundRows
s_AdmDate =格式(CDate(o_Row.Item(AdministeredDate)),KC_Date_Format2)

如果s_LastAdmDate<> s_AdmDate然后
s_LastAdmDate = s_AdmDate
o_AdmDates.Add(s_LastAdmDate)
结束如果
接下来

'将管理日期添加到派生表
Dim o_Item As String
对于每个o_Item在o_AdmDates
o_DerivedTable.Columns.Add(o_Item)
下一个


循环管理日期
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)
如果i_Ctr < > i_MaxRec然后
o_NextRow = o_Dataset.Tables(0).Rows(i_Ctr + 1)
如果
$ b结束如果x_isNewRow然后
o_NewRow = o_DerivedTable.NewRow b $ b 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)=格式KC_Date_Format2)
o_NewRow(newAdmed)= o_Row(newAdmed)


根据查询填充相应的结果日期列
对于每个o_Item在o_AdmDates
s_AdmDate = Format(CDate(o_Row(AdministeredDate)),KC_Date_Format2)
Dim AdmTim As DateTime = DateTime.Parse(o_Row(AdministeredDate))
如果s_AdmDate = o_Item, $ b o_NewRow(s_AdmDate)= AdmTim.ToString(hh:mm tt)+ - + o_Row(UserID)
结束如果
接下来

如果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
结束如果

下一页
dgSheetPrint.DataSource = o_DerivedTable
dgSheetPrint.DataBind()


解决方案

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



代码在此之后



[因为你没有提供生成的html我已经做了类似的表] / p>






 < table id =previousOrderExportsclass =chromeTheme> 
< tr>
< td class =bold>汇出ID< / td>
< td class =bold>请求类型< / td>
< td class =bold>时间范围< / td>
< td class =bold>搜索过滤器< / td>
< td class =bold>搜索字词< / td>
< td class =bold>请求< / td>
< td class =bold>已处理< / td>
< td class =bold>汇出状态< / td>
< td class =bold>下载< / td>
< / tr>
< tr>
< td> 143306< / td>
< td>手动< / 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 PST< / td>
< td>完成< / td>
< td>< a href =fghhsd.dsfd / sfre / 143306>下载< / 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 PST< / td>
< td>完成< / td>
< td>< a href =fghhsd.dsfd / sfre / 142873>下载< / 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>完成< / td>
< td>< a href =fghhsd.dsfd / sfre / 142766>下载< / 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>完成< / td>
< td>< a href =fghhsd.dsfd / sfre / 142752>下载< / 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 PST< / td>
< td>完成< / td>
< td>< a href =fghhsd.dsfd / sfre / 142738>下载< / 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 PST< / td>
< td>完成< / td>
< td>< a href =fghhsd.dsfd / sfre / 142723>下载< / 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 PST< / td>
< td> 12/07/14 07:02:04 PST< / td>
< td>完成< / td>
< td>< a href =fghhsd.dsfd / sfre / 142709>下载< / a>< / td&
< / tr>
< / table>
< input id =Button1type =buttonvalue =Printonclick =print_breakup_table()/>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.jslanguage =javascripttype =text / javascript> / script>
< script language =javascripttype =text / javascript>
函数print_breakup_table()
{
var tableid =previousOrderExports; //在这种情况下,表的id是你的datagrid的cliend side id
var fixedcols = 2; // no。的列0基于索引,将在每一页上重复
var eachpagecols = 3; // no。将在每一页上打印的列
var colCount = 0;
$('#'+ tableid +'tr:nth-​​child(1)td')。每个函数(){
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>



I cannot explain further on the title so kindly base on the links below. I am using DataGrid (not GridView), in ASP.net using VB code.

This is my current display in my datagrid click here to view picture1 , and i want to make it like this clck here to view picture2

My purpose is to split my datagrid into multiple datagrid if i have multiple columns. I need to split them so that I can print it Google Chrome nicely. Because the print preview in picture 1 using javascript's window.print is that it auto-fits my multicolumn in one dataGrid, thus, if i have, let's say, 20columns of data, the spacing of my columns will be small and it will not be readable to users. like this click here to view picture3

if you have suggestions on how to print my datagrid in browser, ill be thankful, 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>

My partial code behind for displaying my data in 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()

解决方案

First I want to say that you are not printing datagrid but the html generated by that. Hence you need to do some thing on the client side. Since you have written you need to break the table and then print i am attaching an html page from where you can copy the code and change it to work with your html generated by the datagrid, you will need to atleast change the id of datagrid.

Code follows here

[since you didn't provided generated html i have made a similar table]

<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>

这篇关于在asp.net中打印多个页面的许多列标题的Datagrid表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆