从 Gridview 单元格获取值

编程入门 行业动态 更新时间:2024-10-27 04:30:35
本文介绍了从 Gridview 单元格获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试从 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/yzLR7s2w

Here is the codebehind for the full class : pastebin/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 10:45:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   Gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!