[英]Getting value from a Gridview cell
我正在嘗試從GridView獲取一個int值。 這是我的gridview的截圖。
這是Gridview的asp代碼
我創建了一個RowCommand方法,當我按下GridView上的Remove按鈕時,它假設給我第二列的int值。 因此,例如,如果我按下最后一個刪除按鈕,它將給我一個int值為5。
這是我的方法的代碼隱藏
protected void grdOrder_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Remove")
{
int bigStore = Convert.ToInt32(grdOrder.SelectedRow.Cells[2].Text);
clsStockCollection AnId = new clsStockCollection();
clsStock TheOrder = new clsStock();
TheOrder.OrderId = bigStore;
AnId.DeleteOrder(TheOrder);
DataCall();
}
}
當我運行此代碼時,我收到此錯誤。 你調用的對象是空的。
我不明白我需要如何或為什么需要引用一個對象,例如:Object As New Object。 為什么我需要這樣做?
以下是完整類的代碼隱藏:pastebin.com/yzLR7s2w
提前謝謝
SelectedRows
僅返回已選擇的行; 沒有選擇你將得不到任何東西。 而不是int bigStore = Convert.ToInt32(grdOrder.SelectedRow.Cells[2].Text);
嘗試:
int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row
int bigStore = Convert.ToInt32(grdOrder.Rows[rowIndex].Cells[2].Text);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.