30列を超えるGridViewがあります。ほとんどはプレーンコントロールですが、一部にはテンプレートコントロール(DropDownList、Calendar、CheckBoxコントロール)を追加しました。問題のコントロールのaspxコードは次のとおりです
<asp:TemplateField HeaderText="Field1 Caption" SortExpression="Field1">
<ItemTemplate>
<asp:Label ID="lblConstructionArea" runat="server" Text='<%# Eval("Field1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlField1" EnableViewState="true" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
ユーザーが[編集]をクリックしたときに、列にドロップダウンが表示されるようにしたかったのです。したがって、このコード(および上記のEditItemTemplate)を追加します
protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
{
string fieldOne = CommonUtils.ExtractControlValue(e,"lblField1",gvData);
gvData.SelectedIndex = e.NewEditIndex;
gvData.EditIndex = e.NewEditIndex;
gvData.DataBind();
BindGridDropDownData(e, CommonUtils.GetConstructionAreas() ,"ddlConstructionArea", constructionArea, "Field1", fieldOne);
}
上記のコードでは、現在利用可能なものを取得して別のメソッドに渡し、ドロップダウンが表示されたときに選択したインデックスを正確に表示できるようにしています。この後、ドロップダウンリストを変更し、GridViewの[更新]ボタンをクリックすると、次のイベントがトリガーされます
protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowEditIndex = e.RowIndex;
GridViewRow gRow = gvData.Rows[rowEditIndex];
DropDownList ddlConstructionArea = (DropDownList) gvData.Rows[rowEditIndex].FindControl("ddlConstructionArea"); //This does not work
ddlConstructionArea = (DropDownList)gRow.FindControl("ddlConstructionArea");//This does not work
ddlConstructionArea = (DropDownList)gvData.Rows[rowEditIndex].Cells[7].FindControl("ddlConstructionArea");//this does not work either
gvData.EditIndex = -1;//this works and the text boxes disappear
gvData.DataBind();//this works and the old data shows up on the gridview
}
バインディングがランタイムであるグリッドで更新を行う方法について興味があります。