从 Gridview 单元格获取值 [英] Getting value from a Gridview cell

查看:23
本文介绍了从 Gridview 单元格获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 GridView 获取 int 值.这是我的 gridview 的屏幕截图.

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

这里是Gridview的asp代码

Here is the asp code for the Gridview

我创建了一个 RowCommand 方法,当我按下 GridView 上的删除按钮时,它应该给我第二列的 int 值.例如,如果我按下最后一个删除按钮,它会给我一个 int 值 5.

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.

这是我的方法的代码隐藏

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.

我不明白我如何或为什么需要引用一个对象,例如:Object As New 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?

这是完整课程的代码隐藏:pastebin.com/yzLR7s2w

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

提前致谢

推荐答案

SelectedRows 只返回被选中的行;没有选择,您将一无所获.而不是 int bigStore = Convert.ToInt32(grdOrder.SelectedRow.Cells[2].Text); 试试:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆