Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在GridView中打印特定行_C#_Asp.net_Gridview_Printing - Fatal编程技术网

C# 在GridView中打印特定行

C# 在GridView中打印特定行,c#,asp.net,gridview,printing,C#,Asp.net,Gridview,Printing,我知道打印整个GridView并不是一个太大的挑战。但是,我希望打印一行。不仅仅是它在gridview中的显示方式,还包括页面上连接在一起的所有行和单元格。每一行都是一份简历,有一个ID、一个姓名列、一个标题列、一个正文列、一个作者列和一个创建日期列。对于body列,仅显示前50个字符-因此仅按显示的方式打印行将不起作用 我希望能够打印该行并将其格式化,使其看起来像一份真实的简历: Name Title Date Body ResumeID 这可能吗

我知道打印整个GridView并不是一个太大的挑战。但是,我希望打印一行。不仅仅是它在gridview中的显示方式,还包括页面上连接在一起的所有行和单元格。每一行都是一份简历,有一个ID、一个姓名列、一个标题列、一个正文列、一个作者列和一个创建日期列。对于body列,仅显示前50个字符-因此仅按显示的方式打印行将不起作用

我希望能够打印该行并将其格式化,使其看起来像一份真实的简历:

Name     Title       Date

         Body


ResumeID
这可能吗

我还想补充一点,每行都有一个打印图标,可以触发
onclick
onclientclick
事件

aspx的一部分:

<div id="GridViewData">
        <asp:GridView ID="GVResume" runat="server" AutoGenerateColumns="False" Width="100%"
             HeaderStyle-BorderWidth="1" HeaderStyle-BorderStyle="Solid"
            OnRowDataBound="GVResume_OnRowDataBound"
            OnRowCommand="GVResume_OnRowCommand">
            <Columns>
                <asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Left"  HeaderText="ID" Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="DateCreated" HeaderText="Date Created" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top"  ItemStyle-Wrap="false" />
                <asp:BoundField DataField="Title" HeaderText="Title" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top"  ItemStyle-Wrap="false" />
                <asp:BoundField DataField="Author" HeaderText="Author" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top"  ItemStyle-Wrap="false" />
                                <asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Left" HeaderText="Body">
                    <ItemTemplate>
                        <asp:Label ID="lblBody" runat="server" Text='<%# Shorten(Convert.ToString(Eval("Body"))) %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top">
                    <ItemTemplate>
                        <asp:ImageButton ID="IBPrint" runat="server" Onclick="IBPrint_Click1"   ImageUrl="~/images/print.png" Height="20" Width="20" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        </div>

aspx.cs的一部分:

  public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }


     protected string Shorten(string Body)
        {
            if (Body.Length > 50)
            {
                Body= Body.Substring(0, 50) + "...";
            }
            return Body;
        }

        private void BindIt()
        {
            GVResume.DataSource = Data.GetInfo(UserID);
            GVResume.DataBind();

        }

    //need to print row and format, not entire gridview
        protected void IBPrint_Click1(object sender, ImageClickEventArgs e)
        {

            GVResume.DataBind();
            StringWriter Writer = new StringWriter();
            HtmlTextWriter HtmlWriter = new HtmlTextWriter(Writer);
            GVResume.RenderControl(HtmlWriter);
            string GrdHtml = Writer.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
            StringBuilder BuildAJsString = new StringBuilder();
            BuildAJsString.Append("<script type = 'text/javascript'>");
            BuildAJsString.Append("window.onload = new function(){");
            BuildAJsString.Append("var print = window.open('', '', 'left=0");
            BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
            BuildAJsString.Append("print.document.write(document.getElementById('GridViewData').innerHTML);");
            BuildAJsString.Append("print.document.close();");
            BuildAJsString.Append("print.focus();");
            BuildAJsString.Append("print.print();};");
            BuildAJsString.Append("</script>");
            ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
            BindIt();
        }
public override void VerifyRenderingInServerForm(控件)
{
//base.VerifyRenderingInServerForm(控件);
}
受保护字符串缩短(字符串正文)
{
如果(体长>50)
{
主体=主体。子串(0,50)+“…”;
}
返回体;
}
私有void BindIt()
{
GVResume.DataSource=Data.GetInfo(UserID);
GVResume.DataBind();
}
//需要打印行和格式,而不是整个gridview
受保护的无效IBPrint_Click1(对象发送者,ImageClickEventArgs e)
{
GVResume.DataBind();
StringWriter编写器=新的StringWriter();
HtmlTextWriter HtmlWriter=新的HtmlTextWriter(编写器);
GVResume.RenderControl(HtmlWriter);
字符串GrdHtml=Writer.ToString().Replace(“\”,“”).Replace(System.Environment.NewLine)”;
StringBuilder BuildAJsString=新建StringBuilder();
BuildAJsString.Append(“”);
BuildAJsString.Append(“window.onload=new function(){”);
BuildAJsString.Append(“var print=window.open(“”,,'left=0”);
Append(“,top=0,width=1000,height=600,status=0');”;
BuildAJsString.Append(“print.document.write(document.getElementById('GridViewData').innerHTML);”;
BuildAJsString.Append(“print.document.close();”;
BuildAJsString.Append(“print.focus();”);
BuildAJsString.Append(“print.print();};”;
BuildAJsString.Append(“”);
RegisterStartupScript(this.GetType(),“GridPrint”,BuildAJsString.ToString());
BindIt();
}

我自己解决了这个问题,基本上只是将行插入gridview下面的一个隐藏div中,然后打印其内部html。我确信还有其他方法,但效果很好:

aspx:



aspx.cs:

 protected void GVResume_RowCommand(object sender, GridViewCommandEventArgs e)
    {

       if(e.CommandName == "Print")
        {

            Label3.Text = Argument;

            PrintIt();

        }
    }

    private void PrintIt()
    {
        GVResume.DataBind();
        StringBuilder BuildAJsString = new StringBuilder();
        BuildAJsString.Append("<script type = 'text/javascript'>");
        BuildAJsString.Append("window.onload = new function(){");
        BuildAJsString.Append("var print = window.open('', '', 'left=0");
        BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
        BuildAJsString.Append("print.document.write(document.getElementById('EmailPrint').innerHTML);");
        BuildAJsString.Append("print.document.close();");
        BuildAJsString.Append("print.focus();");
        BuildAJsString.Append("};"); 
        BuildAJsString.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
        BounceCount();
    }






protected void IBPrint_Click1(object sender, ImageClickEventArgs e)
    {

        GVResume.DataBind();
        StringBuilder BuildAJsString = new StringBuilder();
        BuildAJsString.Append("<script type = 'text/javascript'>");
        BuildAJsString.Append("window.onload = new function(){");
        BuildAJsString.Append("var print = window.open('', '', 'left=0");
        BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
        BuildAJsString.Append("print.document.write(document.getElementById('GridViewData').innerHTML);");
        BuildAJsString.Append("print.document.close();");
        BuildAJsString.Append("print.focus();");
        BuildAJsString.Append("print.print();};");
        BuildAJsString.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
        BounceCount();
    }
protectedvoid GVResume\u行命令(对象发送方,GridViewCommandEventArgs e)
{
如果(例如,CommandName==“打印”)
{
Label3.Text=参数;
PrintIt();
}
}
私有void PrintIt()
{
GVResume.DataBind();
StringBuilder BuildAJsString=新建StringBuilder();
BuildAJsString.Append(“”);
BuildAJsString.Append(“window.onload=new function(){”);
BuildAJsString.Append(“var print=window.open(“”,,'left=0”);
Append(“,top=0,width=1000,height=600,status=0');”;
buildajstring.Append(“print.document.write(document.getElementById('EmailPrint').innerHTML);”;
BuildAJsString.Append(“print.document.close();”;
BuildAJsString.Append(“print.focus();”);
BuildAJsString.Append(“};”);
BuildAJsString.Append(“”);
RegisterStartupScript(this.GetType(),“GridPrint”,BuildAJsString.ToString());
BounceCount();
}
受保护的无效IBPrint_Click1(对象发送者,ImageClickEventArgs e)
{
GVResume.DataBind();
StringBuilder BuildAJsString=新建StringBuilder();
BuildAJsString.Append(“”);
BuildAJsString.Append(“window.onload=new function(){”);
BuildAJsString.Append(“var print=window.open(“”,,'left=0”);
Append(“,top=0,width=1000,height=600,status=0');”;
BuildAJsString.Append(“print.document.write(document.getElementById('GridViewData').innerHTML);”;
BuildAJsString.Append(“print.document.close();”;
BuildAJsString.Append(“print.focus();”);
BuildAJsString.Append(“print.print();};”;
BuildAJsString.Append(“”);
RegisterStartupScript(this.GetType(),“GridPrint”,BuildAJsString.ToString());
BounceCount();
}

是的,这是可能的。首先向我们展示您拥有的代码,然后逐步进行操作。您是否将打印图标添加到GridView?转到Google并搜索“C#ASP.NET GridView打印行”“。请检查第一个结果。@lucidgold-添加了一些代码,如果有帮助的话。”。谢谢我确实查看了大约30个google链接,但毫无用处,我发现了如何按gridview中的显示打印一行,但并没有获得每个元素的全部内容,也没有按照需要对其进行格式化。
 protected void GVResume_RowCommand(object sender, GridViewCommandEventArgs e)
    {

       if(e.CommandName == "Print")
        {

            Label3.Text = Argument;

            PrintIt();

        }
    }

    private void PrintIt()
    {
        GVResume.DataBind();
        StringBuilder BuildAJsString = new StringBuilder();
        BuildAJsString.Append("<script type = 'text/javascript'>");
        BuildAJsString.Append("window.onload = new function(){");
        BuildAJsString.Append("var print = window.open('', '', 'left=0");
        BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
        BuildAJsString.Append("print.document.write(document.getElementById('EmailPrint').innerHTML);");
        BuildAJsString.Append("print.document.close();");
        BuildAJsString.Append("print.focus();");
        BuildAJsString.Append("};"); 
        BuildAJsString.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
        BounceCount();
    }






protected void IBPrint_Click1(object sender, ImageClickEventArgs e)
    {

        GVResume.DataBind();
        StringBuilder BuildAJsString = new StringBuilder();
        BuildAJsString.Append("<script type = 'text/javascript'>");
        BuildAJsString.Append("window.onload = new function(){");
        BuildAJsString.Append("var print = window.open('', '', 'left=0");
        BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
        BuildAJsString.Append("print.document.write(document.getElementById('GridViewData').innerHTML);");
        BuildAJsString.Append("print.document.close();");
        BuildAJsString.Append("print.focus();");
        BuildAJsString.Append("print.print();};");
        BuildAJsString.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
        BounceCount();
    }