从Gridview单元格中获取价值

时间:2022-08-24 11:46:50

I'm trying to get an int value from a GridView. Here is the screenshot of my gridview.

我正在尝试从GridView获取一个int值。这是我的gridview的截图。

从Gridview单元格中获取价值

Here is the asp code for the Gridview

这是Gridview的asp代码

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.

我创建了一个RowCommand方法,当我按下GridView上的Remove按钮时,它假设给我第二列的int值。因此,例如,如果我按下最后一个删除按钮,它将给我一个int值为5。

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();

    }
}

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?

我不明白我需要如何或为什么需要引用一个对象,例如:Object As New Object。为什么我需要这样做?

Here is the codebehind for the full class : pastebin.com/yzLR7s2w

以下是完整类的代码隐藏:pastebin.com/yzLR7s2w

A thanks in advance

提前谢谢

1 个解决方案

#1


7  

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:

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);

#1


7  

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:

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);