从一个GridView单元格中获得价值 [英] Getting value from a Gridview cell
问题描述
我想从一个GridView一个int值。这里是我的GridView的截图。
I'm trying to get an int value from a GridView. Here is the screenshot of my gridview.
下面是ASP code为GridView
Here is the asp code for the Gridview
我已经创建了哪里,当我preSS的GridView中删除按钮是想给我第二列的int值一RowCommand方法。因此,例如,如果我是preSS最后一个删除按钮它会给我的5 int值
I have created a RowCommand method where when I press the Remove button on the GridView it is suppose to give me the int value of the 2nd column. So for example if I was to press the last Remove button it would give me an int value of 5.
这是对我的方法codebehind
This is my codebehind for the method
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();
}
}
当我运行这个code我得到这个错误。
对象引用未设置到对象的实例。
When I run this code I get this error. Object reference not set to an instance of an object.
我不明白为什么我需要引用,例如一个对象:对象作为新对象。我为什么要这么做?
I don't understand how or why I need to reference an object for example : Object As New Object. Why do I need to do this?
下面是完整的类codebehind:pastebin.com/yzLR7s2w
Here is the codebehind for the full class : pastebin.com/yzLR7s2w
提前感谢
推荐答案
SelectedRows
只返回被选中的行;没有选择,你不会得到任何东西。取而代之的 INT BIGSTORE = Convert.ToInt32(grdOrder.SelectedRow.Cells [2]。文本);
尝试:
SelectedRows
only returns rows that have been selected; without a selection you won't get anything. Instead of int bigStore = Convert.ToInt32(grdOrder.SelectedRow.Cells[2].Text);
try:
int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row
int bigStore = Convert.ToInt32(grdOrder.Rows[rowIndex].Cells[2].Text);
这篇关于从一个GridView单元格中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!