2013-10-29 54 views
1
羣雄,排數

我有以下的GridView:麻煩從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控件,每一個在其旁邊有一個「查看」按鈕。點擊'View'時,我試圖使用GridView1_RowCommand,它應該在傳遞到ViewButton_Click之前獲取點擊的行。然後這將調用GetImageData並通過行號到該行:

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

細胞0是SysInvoiceID列,因此,如果正確的行通過,@ID將被分配一個SysInvoiceID。

然而,'行'似乎並不是一個有效的參數,但我想不出爲什麼不... ...除非我必須明確地將其轉換爲int嗎?任何幫助將非常感激!謝謝。

+0

側面說明的:如果' ID'是一個'int',你應該使用'int.Parse(celltext)',否則數據庫會得到錯誤的類型,因爲'AddWithValue'需要從值中推斷出類型。 –

+0

只是注意到了一些東西......在代碼至少工作之前,如果我明確地鍵入一個行號如1,但是目前沒有任何事情發生。從外觀看,我不認爲ViewButton_Click被正確調用? – JimmyK

回答

2

我剛纔評論這是側面說明但也許這是你的問題,因爲你提到「它似乎並沒有一個有效的說法,但我想不出爲什麼不......除非我必須明確地將其轉換爲int「

如果IDint你應該使用int.Parse(celltext),othwerwise數據庫中獲取了錯誤的類型,因爲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

+0

感謝您的信息,我不知道!雖然是的,但這並沒有解決我所遇到的特殊問題。 – JimmyK

+0

@JimmyK:編輯我的答案,你沒有添加事件處理程序'GridView1_RowCommand'和其他一些東西。 –

+0

管理得到這與大家的幫助工作!我會把這個標記爲答案,因爲你給了我最多的支持,非常感謝! – JimmyK

1

使用本:

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

到位

int index = Convert.ToInt32(e.CommandArgument); 
+0

如果它不起作用,刪除.Parent:P –

+0

沒關係修復它,感謝您的幫助! :) – JimmyK