エラー:エラーを投げデータバインド: 'ClassLibrary.SubCategoryModel' の名前のプロパティが含まれていません 'objCat'
DataBinding: 'ClassLibrary.SubCategoryModel' does not contain a property with the name 'objCat'
SubCategories.aspx
<asp:GridView runat="server" ID="gridSubCategories" Height="156px" Width="240px" AutoGenerateColumns="false" AllowPaging="true" PageSize="5" OnPageIndexChanging="gridSubCategories_PageIndexChanging">
<Columns>
<asp:BoundField DataField="SubCategoryID" HeaderText="Sub Category ID" InsertVisible="false" ReadOnly="true" SortExpression="SubCategoryID" />
<asp:BoundField DataField="SubCategoryName" HeaderText="Sub Category" SortExpression="CategoryName" />
<asp:BoundField DataField="objCat.CategoryName" HeaderText="Category" InsertVisible="false" ReadOnly="true" SortExpression="objCat.CategoryName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" CommandName="EnableEdit" CommandArgument="<%#((GridViewRow)Container).RowIndex%>" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" CommandName="DeleteCategory" CommandArgument="<%#((GridViewRow)Container).RowIndex%>" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
のグリッド表示制御コード:
if (!IsPostBack)
{
gridSubCategories.DataSource = sda.GetSubCategories();
gridSubCategories.DataBind();
}
クラス定義:
public class CategoryModel
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
public class SubCategoryModel
{
public CategoryModel objCat = new CategoryModel();
public int SubCategoryID { get; set; }
public string SubCategoryName { get; set; }
}
GetSubCategories方法:
public List<SubCategoryModel> GetSubCategories()
{
List<SubCategoryModel> listSubCatList = new List<SubCategoryModel>();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("GetSubCategories"))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
SubCategoryModel subcatObj = new SubCategoryModel();
subcatObj.SubCategoryID = Convert.ToInt32(sdr["SubCategoryID"]);
//subcatObj.objCat.CategoryID = Convert.ToInt32(sdr["CategoryID"]);
subcatObj.objCat.CategoryName = sdr["CategoryName"].ToString();
subcatObj.SubCategoryName = sdr["SubCategoryName"].ToString();
listSubCatList.Add(subcatObj);
}
con.Close();
return listSubCatList;
}
}
}
私は、データをフェッチしてとGridViewののデータソースに渡されることがわかります。 Contents of DataSource property of the gridview
なぜobjCatがCategoryModelクラス内のプロパティとして識別されていませんか?オブジェクトの中にオブジェクトをバインドする他の方法はありますか?