如何为嵌套网格视图中的控件赋值?

时间:2015-02-05 16:03:51

标签: c# asp.net gridview

我有父网格视图名为' gvParentGrid'在我内部有嵌套的gridiview,名为' gvChildGrid'。我在父gridview中创建了一个行数据绑定事件 - gvParentGrid。

我尝试将label11分配给我的sql查询输出。在下面的代码中,我尝试将rDate分配给label11变量,但它不断抛出 - 对象引用未设置为对象的实例。错误。

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            con.Open();
            GridView gv = (GridView)e.Row.FindControl("gvChildGrid");

            Label lblorderID = (Label)e.Row.FindControl("Label2");

            Label lblRead = (Label)gv.FindControl("Label11");

            string userID = lblorderID.Text;

            string cmdstr = "select top 2 [rUID], count([rDATE]) as 'articles read' from ArticlesRead where [rUID] = @ID group by [rUID] order by count([rDATE]) desc";
            SqlCommand cmd = new SqlCommand(cmdstr, con);
            cmd.Parameters.AddWithValue("@ID", userID);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                // Line below causing the object reference error.
                ***lblRead.Text = ds.Tables[0].Rows[0][1].ToString();***
            }

            con.Close();

            gv.DataSource = ds;
            gv.DataBind();
        }

以下是客户端模板以供进一步参考:

 <asp:GridView ID="gvParentGrid" runat="server" DataKeyNames="uID"  Width="300" AutoGenerateColumns="false" OnRowDataBound="gvUserInfo_RowDataBound">
 <asp:TemplateField HeaderText="ID" SortExpression="ID">
 <HeaderTemplate>
 <asp:Label ID="Label1" runat="server" Text="ID"></asp:Label><br />
 </HeaderTemplate>
 <ItemTemplate>
 <asp:Label ID="Label2" runat="server" Text='<%# Bind("uID") %>' >   </asp:Label>
</ItemTemplate>
</asp:TemplateField>
 <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="uID">
<asp:TemplateField HeaderText="Articles Read">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>

非常感谢任何进一步的建议。感谢

0 个答案:

没有答案