I got an error of
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
Source Error:
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";
my code
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;
}
the error pointing
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
Row.Cells[0].Controls[2]
line but you sayRow.Cells[0].Controls[0]
line? – Soner Gönülasp-classic
please re-tag. – Paule.Row.Cells
contains any elements? If it does, can you also verify thate.Row.Cells[0].Controls
contains any elements? Also, just a nitpick, but in theGridView1_RowDataBound
handler, the inner if-statements are duplicated. – Jason Z