itemtemplate
GridViewにあるドロップダウン リストをバインドしようとしています。しかし、上記の「インデックス1が負であるか、行数を超えています」というエラーが表示されます。
私の GridView には最大 100 行が含まれており、各ドロップダウン リストを特定のデータにバインドしたいと考えています。でエラーが発生し、 GridView とメイン データをバインドするメソッドonRowDataBoundEvent
で例外 (デバッグ ツールによる) が出力されています。GridBinding
コードは次のとおりです。
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
DropDownList _ddtpcs = (DropDownList)e.Row.Cells[4].FindControl("_ddtsttpc");
if (_ddtstsubs.SelectedIndex != 0 && _ddtstsubs.SelectedIndex != -1)
{
if (_ddtpcs != null)
{
_ddtpcs.DataSource = _adl.LoadTopics(long.Parse(_ddtstsubs.SelectedValue));
_ddtpcs.DataTextField = "top_nm";
_ddtpcs.DataValueField = "top_id";
_ddtpcs.DataBind();
_ddtpcs.Items.Insert(0, new ListItem("Select", "0"));
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", POPUPBuilder.ShowPopUpMsg(ex.ToString()), true);
}
}
グリッドバインディングのコードは次のとおりです。
_grdqans.DataSource = _adl.LoadQans(long.Parse(_hdntstId.Value));
_grdqans.DataBind();
GridView のドロップダウン リストは次のとおりです。
<ItemTemplate>
<asp:DropDownList ID="_ddtsttpc" runat="server">
</asp:DropDownList>
</ItemTemplate>
LoadTopics メソッド
internal object LoadTopics(long _subId)
{
try
{
cmd = new SqlCommand("sp_LoadTopics", con.getconnection());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@subId", _subId);
da = new SqlDataAdapter(cmd);
dt.Clear();
da.Fill(dt);
cmd.Parameters.Clear();
cmd.Connection.Close();
cmd.Dispose();
//da.Dispose();
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.closeconnection();
}
}
ここで何が間違っていますか?