开发者

How to bind data in gridview in asp.net without using properties

I have this code I want to bind data to grid view without using properties .

        public class UserTerritory
        {
           public string TerrId;
           public string TerrName;
           public string AccAccessLevel;

           public UserTerritory(string _TerrId, string _TerrName, string _AccAccessLevel)
           {
              this.TerrId = _TerrId;
              this.TerrName = _TerrName;
              this.AccAccessLevel= _AccAccessLevel;
           }
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        List<UserTerritory> ut = new List<UserTerritory>();
        ut.Add(new UserTerritory("1", "x", "a"));
       开发者_如何学Python ut.Add(new UserTerritory("2", "y", "b"));
        ut.Add(new UserTerritory("3", "z", "c"));



        grdUserTerr.DataSource = ut;
        grdUserTerr.DataBind();
    }

When I execute the above code I get following Httpexception " The data source for GridView with id 'grdUserTerr' did not have any properties or attributes from which to generate columns. Ensure that your data source has content."

Can Somebody tell me what wrong I am doing ?

Thanks for your reply


The problem is that databinding uses DataBinder.Eval (or just Eval ) behind the scenes and it looks only for properties. They aren't exactly the same as public members (fields). Properties are closer to methods than fields are.

So why don't you just use properties like this?

public string AccAccessLevel { get; set; }    
public string TerrName  { get; set; }    
public string TerrId { get; set; }


there is problem some where in the property decalration section please change the properties as private memeber it will works for you.

 public class UserTerritory
        {
           private string TerrId;
           private string TerrName;
           private string AccAccessLevel;

           public UserTerritory(string _TerrId, string _TerrName, string _AccAccessLevel)
           {
              this.TerrId = _TerrId;
              this.TerrName = _TerrName;
              this.AccAccessLevel= _AccAccessLevel;
           }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜