列表和 GridView 绑定
这是我班级的一些属性。还有另一个类的公共对象 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);
}
Here are a few properties of my class. There is also a public object of another class 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();
In the same class I have a method returnig List. Besides my regular fields, I'm also populating one field of my DNRole object:
listItem.DNRole.RoleName = dr["RoleNameFK"].ToString();
My question is.. Is this legal and is it how it should be done, and if yes, how do I show data in a 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ObjectDataSource - 来包装 SelectForGrid 调用,也可以将 List 直接绑定到 GridView:
当数据绑定在 *.aspx/ascx 代码中时,您需要将列绑定到属性:
Eval 和 Bind 允许您按名称执行此操作,但使用显式转换为 MarketingCampaign 的方式更快。
...
You can use ObjectDataSource - to wrap call SelectForGrid, also you can direct bind List to your GridView:
When data is binded in *.aspx/ascx code you need bind columns to properties:
Eval and Bind allows you do it by name, but faster way to use explicit cast to MarketingCampaign.
...