-1

i want to bind gridview with my custom entity whitch i populate from data base but i get error Object reference not set to an instance of an object i know the error is from my gridlink class when select new and want to set to link property in this行:link = { linkName = "tyr", linkSrc = "ytr" }, 因为当我删除它时,错误停止和gridview绑定谢谢

  public class gridcolumns
{

    public decimal cost { get; set; }
    public Int32 count { get; set; }
    public gridlink link { get; set; }

    public gridcolumns()
    {

        // TODO: Complete member initialization
    }
}

public class gridlink
{
    public string linkName { get; set; }
    public string linkSrc { get; set; }

    public gridlink()
    {

    }
}
 protected void Page_Load(object sender, EventArgs e)
{
    Data281DataContextDataContext conx = new Data281DataContextDataContext();
    List<tbl_2_CheckReqNo_NotValid> allresult = conx.tbl_2_CheckReqNo_NotValids.ToList();
    gridcolumns lastMantWithDate = new gridcolumns();
    if (Request.QueryString.Count == 0)
    {
        var lastMantWithDaste = from pe in allresult //where allresult != null
                                orderby Convert.ToDecimal(pe.mandeh) descending
                                group pe by pe.mant into grouped
                                where grouped != null
                                select new gridcolumns
                                {
                                    link = { linkName = "tyr", linkSrc = "ytr" },
                                    cost = grouped.Sum(g => Convert.ToDecimal(g.mandeh)),
                                    count = grouped.Count(),

                                };

        GrdOstan.DataSource = lastMantWithDaste;
        GrdOstan.DataBind();
    }
4

4 回答 4

4

您应该gridlink像这样实例化:

link = new gridlink { linkName = "tyr", linkSrc = "ytr" },

考虑一下,对于您的查询产生的每个项目,您都会创建一个gridcolumns对象。该link对象的属性最初是null; 您应该new gridlink在尝试使用它之前将其设置为。

于 2012-08-29T07:34:41.270 回答
3

你可能需要

link = new gridLink { linkName = "tyr", linkSrc = "ytr" }

代替

link = { linkName = "tyr", linkSrc = "ytr" }
于 2012-08-29T07:34:36.620 回答
1

替换这个:

select new gridcolumns
           {
           link = { linkName = "tyr", linkSrc = "ytr" },

和:

select new gridcolumns
           {
           link = new gridlink  { linkName = "tyr", linkSrc = "ytr" },

您需要gridLink使用new关键字实例化类型的新对象

于 2012-08-29T07:36:31.923 回答
0

在您的行中:

link = { linkName = "tyr", linkSrc = "ytr" }

你需要“新的gridlink”,像这样:

link = new gridlink { linkName = "tyr", linkSrc = "ytr" }

就个人而言,我认为您的代码编译是不幸的。如果我尝试:

List<gridlink> links = new List<gridlink>();
links.Add({ linkName = "tyr", linkSrc = "ytr" });

我会正确地得到一堆语法错误。

添加new gridlink应该可以解决这个问题。

于 2012-08-29T07:47:37.657 回答