2013-10-29 51 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