RowIndex不在范围内

本文关键字:范围内 RowIndex | 更新日期: 2023-09-27 18:17:51

我的代码有一些问题。我试图为每种情况展示不同的画面。但是代码似乎是有缺陷的,我是c#和ASP.net的新手,所以我很容易迷路。下面是崩溃的那部分代码:

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        string Class = (e.Row.FindControl("txtClass") as TextBox).Text;
        HtmlControl htmctrl = e.Row.FindControl("imgid") as HtmlControl;
        switch (Class)
        {
            case "A1":
                {
                    string Logo = @"C:'Users'Rudra'documents'visual studio 2010'Projects'MvcApplication1'MvcApplication1'Images'Box_Blue.png";
                    htmctrl.Attributes.Add("src", Logo);
                    break;
                }
            case "A2":
                {
                    string Logo = @"C:'Users'Rudra'documents'visual studio 2010'Projects'MvcApplication1'MvcApplication1'Images'Box_Green.png";
                    htmctrl.Attributes.Add("src", Logo);
                    break;
                }
            case "A3":
                {
                    string Logo = @"C:'Users'Rudra'documents'visual studio 2010'Projects'MvcApplication1'MvcApplication1'Images'Box_Red.png";
                    htmctrl.Attributes.Add("src", Logo);
                    break;
                }
            default:
                {
                    string Logo = @"C:'Users'Rudra'documents'visual studio 2010'Projects'MvcApplication1'MvcApplication1'Images'not-found.png";
                    htmctrl.Attributes.Add("src", Logo);
                    break;
                }
        }
    }

crash发生在第一行GridViewRow row = GridView1.Rows[e.RowIndex];RowIndex未被识别(不显示在智能感知上)。如果你能看出我哪里出错了,为什么会崩溃,以及如何修复,我将永远感激不尽。

RowIndex不在范围内

不需要转换

GridViewRow row = GridView1.Rows[e.RowIndex];

你可以直接使用

GridViewRow row = e.Row;