![](/img/trans.png)
[英]Exception: Specified argument was out of the range of valid values. (Parameter 'index')
[英]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.