繁体   English   中英

错误指定的参数超出了有效值的范围。 参数名称:DataGridview Row Data Bound 中的索引

[英]Error Specified argument was out of the range of valid values. Parameter name: index in DataGridview Row Data Bound

我有一个错误

说明:在执行当前 Web 请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。 参数名称:索引

源错误:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[2];
    string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
    e.Row.Style["cursor"] = "hand";

我的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("itemid", typeof(string)),
                new DataColumn("itemdesc", typeof(string)),
                new DataColumn("itemtype",typeof(string)) });
        dt.Rows.Add("FG001", "Red Velvet Cake (8'' round)","Dry Goods");
        dt.Rows.Add("FG002", "Voilet Velvet Cake (8'' round)", "Dry Goods");
        GridView1.DataSource = dt;
        GridView1.DataBind();  
    }
}

protected void OnDataBound(object sender, EventArgs e)
{
    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    for (int i = 0; i < GridView1.Columns.Count; i++)
    {
        TableHeaderCell cell = new TableHeaderCell();
        TextBox txtSearch = new TextBox();
        txtSearch.Attributes["placeholder"] = GridView1.Columns[i].HeaderText;
        txtSearch.CssClass = "search_textbox";

        cell.Controls.Add(txtSearch);
        row.Controls.Add(cell);
    }

    GridView1.HeaderRow.Parent.Controls.AddAt(1, row);
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (!IsPostBack)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
            string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
            e.Row.Style["cursor"] = "hand";
            e.Row.Attributes["onclick"] = _jsSingle;
        } 
    }
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow selectedRow = GridView1.SelectedRow;
    hiddenitemid.Value = selectedRow.Cells[0].Text;
}

错误指向

LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];

要么有没有任何单元格的行,要么有少于三个控件的单元格。

为了防止代码崩溃,只需检查您是否有足够的项目:

if (e.Row.Cells.Count > 0)
{
    TableCell cell = e.Row.Cells[0];
    if (cell.Controls.Count > 2)
    {
        LinkButton _singleClickButton = (LinkButton)cell.Controls[2];
        string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
        e.Row.Style["cursor"] = "hand";
    }
}

此错误背后的原因是当您的代码执行时,您的 gridview 中没有可用的单元格。

您正在向 gridview 添加列并将其绑定到 page_load 中的数据表,也在"if(!this.IsPostBack)"块中。 因此,只有在第一次加载页面时,才会将列添加到 gridview。 当 gridview 事件被触发并且页面被回发时,page_load 会再次执行,但没有添加列,也没有对 gridview 进行数据绑定。

在执行 gridview 的事件处理程序之后,它在行中找不到任何单元格(因为网格中没有列),因此它不会进入"if (e.Row.Cells.Count > 0)"块。 如果您尝试使用 Cells[0] 访问单元格,则会引发异常。

这个问题的解决方法是在HTML中定义gridview的列结构,在后面的代码中将gridview绑定到数据源。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="itemdesc" HeaderText="Item Description" ReadOnly="True" />
        <asp:BoundField DataField="itemtype" HeaderText="Item Type" ReadOnly="True" />
    </Columns>
</asp:GridView>

或者您在每次页面加载时执行的 InitialzeComponents() 方法中编写向 gridview 添加列的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM