如何从GridView将参数传递给Crystal Report?

时间:2012-08-14 19:52:25

标签: asp.net .net crystal-reports webforms

我有一个现有的SQL数据库和一个ASP.NET应用程序。我的应用程序有两个现有的GridViews和登录功能。我还有一个现有的Crystal Report,旨在从我的SQL数据库中自动创建收据。这是通过用户填写3个特定参数来完成的,其余数据(与这些参数并行)将自动填写水晶报告。

我想在GridView中创建一个打印按钮,以自动填写Crystal Report中的3个参数。这是为了使我的应用程序更加用户友好。简而言之,用户可以在GridView的新列中按下打印按钮,3个参数将自动被拾取并填充到Crystal Report中。

我的参数是:“EmpID”,“KeyControl”和“ControlNumber”。我的水晶报告标签是“x.rpt”

这是我的GridView标记:

<asp:GridView ID="gridKeyAndBuildingInformation" runat="server"  CssClass="style3" 
              AllowSorting ="True" 
              AutoGenerateColumns ="False" 
              AllowPaging="True"
              DataKeyNames="KeyRefId"
              OnRowCancelingEdit="gridKeyAndBuildingInformation_RowCancelingEdit" 
              onPageIndexChanging="gridKeyAndBuildingInformation_PageIndexChanging"
              OnRowDataBound="gridKeyAndBuildingInformation_RowDataBound"
              OnRowEditing="gridKeyAndBuildingInformation_RowEditing" 
              OnRowUpdating="gridKeyAndBuildingInformation_RowUpdating" 
              OnRowCommand="gridKeyAndBuildingInformation_RowCommand" 
              ShowFooter="True"
              OnRowDeleting="gridKeyAndBuildingInformation_RowDeleting"                                    
              AlternatingRowStyle-BackColor="#EFEFEF" 
              EditRowStyle-VerticalAlign="Top" 
              HeaderStyle-BackColor="#77b218"
              OnSorting="gridKeyAndBuildingInformation_Sorting" 
              BackColor="#CCCCCC" 
              BorderColor="#999999" 
              BorderStyle="Solid" 
              BorderWidth="3px" 
              CellPadding="4" 
              EnableModelValidation="True" 
              ForeColor="Black" 
              CellSpacing="2">
<Columns>

<asp:TemplateField HeaderText ="EmpID" HeaderStyle-CssClass="HeaderText" sortexpression="EmpID">
    <ItemTemplate>                     
    <asp:Label ID="lblEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate> 
    <asp:TextBox ID="txtEmpID" runat="server" Text='<%# Eval("EmpID") %>' Width="50px"></asp:TextBox> 
</EditItemTemplate> 
<FooterTemplate> 
    <asp:TextBox ID="txtNewEmpID" runat="server"  Width="50px"></asp:TextBox> 
</FooterTemplate> 
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>

<asp:TemplateField HeaderText ="ControlNumber" HeaderStyle-CssClass="HeaderText" sortexpression="ControlNumber">
<ItemTemplate>                     
    <asp:Label ID="lblControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate> 
    <asp:TextBox ID="txtControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>' Width="50px"></asp:TextBox> 
</EditItemTemplate> 
<FooterTemplate> 
    <asp:TextBox ID="txtNewControlNumber" runat="server"  Width="50px"></asp:TextBox> 
</FooterTemplate> 
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField> 

<asp:TemplateField HeaderText ="KeyNumber" HeaderStyle-CssClass="HeaderText" sortexpression="KeyNumber">
<ItemTemplate>                     
    <asp:Label ID="lblKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate> 
    <asp:TextBox ID="txtKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>' Width="50px"></asp:TextBox> 
</EditItemTemplate> 
<FooterTemplate> 
    <asp:TextBox ID="txtNewKeyNumber" runat="server"  Width="50px"></asp:TextBox> 
</FooterTemplate> 
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField> 

<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> 
    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
</EditItemTemplate> 
<FooterTemplate>
    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> 
</FooterTemplate> 
<ItemTemplate> 
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> 
</ItemTemplate> 
</asp:TemplateField> 

<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> 
<asp:ButtonField HeaderText="Print" ShowHeader="True" Text="Print" />

</Columns>

<EditRowStyle VerticalAlign="Top"></EditRowStyle>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>

1 个答案:

答案 0 :(得分:1)

您可以处理RowCommand事件,当您点击ButtonField列中的删除按钮时会触发该事件。将其添加到GridView声明标记的末尾(在“CellSpacing="2"”之后但在“>”之前):

OnRowCommand="gridKeyAndBuildingInformation_RowCommand"

然后,在你的代码背后,你需要这样的东西(这是C#,如果你需要VB.NET让我知道 - 这个问题没有用服务器端语言标记):

protected void gridKeyAndBuildingInformation_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // Get your ID for the row you're on
    int ID = Convert.ToInt32(e.CommandArgument);

    // Get the row the button was clicked in
    GridViewRow row = gridKeyAndBuildingInformation.Rows[ID];

    // Get the values you need from that row
    int EmpID = row.Cells[0];
    int ControlNumber = row.Cells[1];
    int KeyNumber = row.Cells[2]; 

    // Use those numbers to make your call to the Crystal Report
    // I don't know what this part would look like.
}

您可以在MSDN上阅读有关RowCommand事件的更多信息:GridView.RowCommand Event

相关问题