以下是我班级的一些属性。还有另一个类 DNRole
的公共对象 private Int32 _IDMarketingCampaign;
private String _Name;
public Int32 IDMarketingCampaign
{
get { return _IDMarketingCampaign; }
set { _IDMarketingCampaign = value; }
}
public String Name
{
get { return _Name; }
set { _Name = value; }
}
public DPRole DNRole = new DPRole();
在同一个类中,我有一个方法returnig List。除了我的常规字段,我还填充了我的DNRole对象的一个字段:
listItem.DNRole.RoleName = dr [“RoleNameFK”]。ToString();
我的问题是......这是否合法,是否应该如何完成,如果是,我如何在girdview中显示数据?
public List<MarketingCampaign> SelectForGrid(string strRoleName)
{
List<MarketingCampaign> lista = new List<MarketingCampaign>();
SqlCommand command = new SqlCommand("fra_MarketingCampaignSelectForGrid ", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("RoleName", SqlDbType.VarChar).Value = strRoleName;
SqlDataReader dr = null;
try
{
conn.Open();
dr = command.ExecuteReader();
while (dr.Read())
{
MarketingCampaign listItem = new MarketingCampaign();
listItem.IDMarketingCampaign = Convert.ToInt32(dr["IDMarketingCampaign"]);
listItem.Name = dr["Name"].ToString();
listItem.DNRole.RoleName = dr["RoleNameFK"].ToString();
...
lista.Add(listItem);
}
答案 0 :(得分:3)
你可以使用ObjectDataSource - 来包装调用SelectForGrid,你也可以直接将List绑定到你的GridView:
MyGrid.DataSoure = some. SelectForGrid();
MyGrid.DataBind();
当数据以* .aspx / ascx代码绑定时,您需要将列绑定到属性: Eval和Bind允许您按名称进行,但更快的方式使用显式强制转换为MarketingCampaign。
<asp:TemplateField >
<ItemTemplate>
<%#((MarketingCampaign)Container.DataItem).Name%>
</ItemTemplate>
...
答案 1 :(得分:0)
List<MarketingCampaign> mk= obj.SelectForGrid("Role");
GridView1.DataSource = mk;
GridView1.DataBind();