1

我有以下 GridView:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="SysInvoiceID" HeaderText="SysInvoiceID" ReadOnly="True" SortExpression="SysInvoiceID" />
                <asp:BoundField DataField="BillMonth" HeaderText="BillMonth" SortExpression="BillMonth" />
                <asp:BoundField DataField="InvoiceDate" HeaderText="InvoiceDate" ReadOnly="True" SortExpression="InvoiceDate" />
                <asp:BoundField DataField="InvoiceNumber" HeaderText="InvoiceNumber" SortExpression="InvoiceNumber" />
                <asp:BoundField DataField="Net" HeaderText="Net" SortExpression="Net" />
                <asp:BoundField DataField="VAT" HeaderText="VAT" SortExpression="VAT" />
                <asp:BoundField DataField="Gross" HeaderText="Gross" SortExpression="Gross" />
                <asp:ButtonField CommandName="ViewInvoice"  HeaderText=" " ShowHeader="True" Text="View" />
            </Columns>
        </asp:GridView>

这是页面背后的代码:

public partial class PagingTest01 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if (e.CommandName == "ViewInvoice")
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection.
        GridViewRow row = GridView1.Rows[index];
        // Now you have access to the gridviewrow.

        ViewButton_Click(row);
    }
}




protected void ViewButton_Click(GridViewRow row)
{ 
    byte[] FileImage = GetImageData(0,row);

      if (FileImage != null)
      {
          base.Response.Clear();
          base.Response.Buffer = true;
          base.Response.ContentType = "Application/x-pdf";
          base.Response.ContentEncoding = Encoding.Default;
          string attachment = string.Format("attachment;filename=\"Invoice_{0}.pdf\"", "Customer1");
          base.Response.AddHeader("content-disposition", attachment);
          base.Response.BinaryWrite(FileImage);
          base.Response.Flush();
          base.Response.Close();
          base.Response.End();
      }
}





public byte[] GetImageData(int sysInvoiceID,  GridViewRow row)
    {

        string strUserID = CommonCode.GetCurrentUserID().ToString();
        string strCustomerID = CommonCode.GetCurrentCustomerID().ToString();
        byte[] numArray;

        string strConnectionString = "Data Source=TESTSERV;Initial Catalog=DB_Invoices;Persist Security Info=True";
        SqlConnection connection = new SqlConnection(strConnectionString);
        SqlCommand command = new SqlCommand("select FileImage from DB_Invoices.dbo.Bills WHERE (FileType = 'PDF' AND SysInvoiceID = @ID)", connection);
        command.Parameters.AddWithValue("@ID", GridView1.Rows[row].Cells[0].Text);

        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();

        try
        {
            connection.Open();



            da.Fill(ds);
            DataRow item = ds.Tables[0].Rows[0];
            byte[] item1 = (byte[])item["FileImage"];
            ds.Tables.Clear();
            numArray = item1;


        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }
        return numArray;
    }



}

所以基本上我有一个 GridView 有很多行,每一行旁边都有一个“视图”按钮域。单击“查看”时,我尝试使用 GridView1_RowCommand,它应该希望在将单击的行传递到 ViewButton_Click 之前抓住单击的行。然后这将调用 GetImageData 并将行号传递到此行:

command.Parameters.AddWithValue("@ID", GridView1.Rows[row].Cells[0].Text);

单元格 0 是 SysInvoiceID 列,因此如果传递了正确的行,@ID 将被分配一个 SysInvoiceID。

但是,“行”似乎不是一个有效的论点,尽管我想不出为什么不...除非我必须将其显式转换为整数?任何帮助将非常感激!谢谢。

4

2 回答 2

2

我刚刚将此评论为旁注,但也许这是您的问题,因为您提到“这似乎不是一个有效的论点,尽管我想不出为什么不……除非我必须明确地将其转换为诠释”

如果IDint你应该使用int.Parse(celltext)的,否则数据库会得到错误的类型,因为AddWithValue需要从值推断类型。

所以使用:

command.Parameters.AddWithValue("@ID", int.Parse(GridView1.Rows[row].Cells[0].Text));

除此之外,您还没有添加事件处理程序GridView1_RowCommand

<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
   ....

而且您也没有将 设置为CommandArgument行的索引。如果您需要行索引,无论如何我都会使用不同的方法。使用模板字段和控件,例如Button. 然后使用它的NamingContainer属性来获取对“GridViewRow”的引用,这里是一个例子:Get Row Index on Asp.net Rowcommand event

于 2013-10-29T10:10:48.813 回答
1

用这个:

int index = ((GridViewRow)((WebControl)sender)).RowIndex;

代替

int index = Convert.ToInt32(e.CommandArgument);
于 2013-10-29T10:17:19.707 回答