[英]Trouble grabbing Row Number from 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”Buttonfield。 單擊“查看”時,我嘗試使用GridView1_RowCommand,希望在將其傳遞到ViewButton_Click之前抓取單擊的行。 然后,這將調用GetImageData並將行號傳遞到此行:
command.Parameters.AddWithValue("@ID", GridView1.Rows[row].Cells[0].Text);
單元格0是SysInvoiceID列,因此如果傳遞了正確的行,則會為@ID分配SysInvoiceID。
然而,'Row'似乎不是一個有效的參數,雖然我想不出為什么不...除非我必須明確地將它轉換為int? 任何幫助將非常感激! 謝謝。
我剛剛將此作為旁注注釋,但也許這是你的問題,因為你提到“它似乎不是一個有效的論據,雖然我不能想到為什么不......除非我必須明確地將它轉換成一個int“ 。
如果ID
是一個int
你應該使用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`的引用,這是一個示例: 在Asp.net上獲取行索引Rowcommand事件
用這個:
int index = ((GridViewRow)((WebControl)sender)).RowIndex;
代替
int index = Convert.ToInt32(e.CommandArgument);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.