嵌套网格视图中的错误“在容器中找不到数据项”

时间:2012-10-19 05:02:08

标签: asp.net linq

在主网格视图"GridViewTtransmittals"中,我有一个名为"GridViewTranstoCon"的嵌套网格视图。当“GridViewTranstoCon”有数据时,我没有问题,但是当没有任何数据时,它会返回错误"A data item was not found in the container",我喜欢它返回null值或让它不可见。 代码如下:

protected void GridViewTtransmittals_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.DataItem == null)
        return;
    TransmittaltoConPresentationModel transmittaltoCon = (TransmittaltoConPresentationModel)e.Row.DataItem;
    GridView g2 = (GridView)e.Row.FindControl("GridViewTranstoCon");

    if (transmittaltoCon.TranstoCons != null)
    {
        g2.DataSource = transmittaltoCon.TranstoCons;
        g2.DataBind();
    }
}

主网格视图“GridViewTtransmittals”的数据源代码来自以下代码:

private void DisplayTransmittals()
{
    if (_Transmittals.Any())
    {
        var query = from transmittalno in _Transmittals

        select new TransmittaltoConPresentationModel { TransID = transmittalno.TransID,
                   Transmittal = transmittalno.TRANSMITTAL, TranstoCons = from doctranstocon in _DocTranstoCons 
                   where doctranstocon == null || transmittalno.TransID == doctranstocon.Transid
                   select doctranstocon != null ? doctranstocon.tblTranstoCon : null};

        GridViewTtransmittals.DataSource = query;
    }
    else
    {
        GridViewTtransmittals.DataSource = null;
    }
    GridViewTtransmittals.DataBind();
}

1 个答案:

答案 0 :(得分:0)

尝试包含此

protected void GridViewTtransmittals_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if(e.Row.RowType == DataControlRowType.DataRow)
   {

   }
}