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

问题描述 投票:0回答:2

我得到了一个错误

描述:执行过程中发生未处理的异常 当前的网络请求。请查看堆栈跟踪以了解更多信息 有关错误及其在代码中的来源的信息。

异常详细信息: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];
c# asp.net gridview
2个回答
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";
    }
}

0
投票

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

您正在向 gridview 添加列,并将其绑定到 page_load 中的数据表,这也在

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

执行完 gridview 的事件处理程序后,它在行中找不到任何单元格(因为网格中没有列),因此它不会进入

"if (e.Row.Cells.Count > 0)"
块内。如果您尝试使用 Cells[0] 访问单元格,则会抛出异常。

解决这个问题的方法是在HTML中定义grid view的列结构,并在后面的代码中将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添加列的代码。

© www.soinside.com 2019 - 2024. All rights reserved.