列表和 GridView 绑定

发布于 2024-08-02 15:27:45 字数 1651 浏览 4 评论 0原文

这是我班级的一些属性。还有另一个类的公共对象 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟月 2024-08-09 15:27:45

您可以使用 ObjectDataSource - 来包装 SelectForGrid 调用,也可以将 List 直接绑定到 GridView:

MyGrid.DataSoure = some. SelectForGrid();
MyGrid.DataBind();

当数据绑定在 *.aspx/ascx 代码中时,您需要将列绑定到属性:
Eval 和 Bind 允许您按名称执行此操作,但使用显式转换为 MarketingCampaign 的方式更快。

<asp:TemplateField >
     <ItemTemplate> 
        <%#((MarketingCampaign)Container.DataItem).Name%> 
     </ItemTemplate>

...

You can use ObjectDataSource - to wrap call SelectForGrid, also you can direct bind List to your GridView:

MyGrid.DataSoure = some. SelectForGrid();
MyGrid.DataBind();

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.

<asp:TemplateField >
     <ItemTemplate> 
        <%#((MarketingCampaign)Container.DataItem).Name%> 
     </ItemTemplate>

...

陈年往事 2024-08-09 15:27:45
List<MarketingCampaign> mk= obj.SelectForGrid("Role");
GridView1.DataSource = mk;
GridView1.DataBind();
List<MarketingCampaign> mk= obj.SelectForGrid("Role");
GridView1.DataSource = mk;
GridView1.DataBind();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文