本文介绍了在ADO.NET中以编程方式嵌套网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式实现嵌套的网格视图。其中两个网格视图彼此相互链接(例如,头部和子头部表)。我正在使用实体框架。现在这个代码中的问题是没有根据相应的头部出现正确的子标题。



i want to implement nested gridviews programatically. in which both gridviews are interlinked with each other(e.g. Heads and Sub-heads tables). I am using entity framework. Now problem in this code is that no proper subheads are appearing according to corresponding heads.

<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>


    </div>
    </form>
</body>










using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace nested_gridview
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hitechLatestEntities database = new hitechLatestEntities();
              
            GridView1.DataSource = database.HEADs;
       
            TemplateField tfObject = new TemplateField();
            tfObject.HeaderText = "Sub-Heads";
            tfObject.HeaderStyle.Width = Unit.Percentage(50);

            tfObject.ItemTemplate = new myTemplate(ListItemType.Item);
            
            GridView1.Columns.Add(tfObject);
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                hitechLatestEntities data = new hitechLatestEntities();
                var result = from t in data.SUB_HEAD
                             join x in data.HEADs on t.head_code equals x.head_code
                             select t;
                string headCode = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                GridView sub = e.Row.FindControl("newgrid") as GridView;
                sub.DataSource = result;
                sub.DataBind();
            }
        }
    }

    //////////////////////// Class for template field //////////////////////////
    public class myTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {            
            if (myListItemType == ListItemType.Item)
            {
                hitechLatestEntities data = new hitechLatestEntities();
                GridView newgrid = new GridView();
                
                newgrid.DataSource = data.SUB_HEAD;                
                container.Controls.Add(newgrid);
            }
        }
        
        private ListItemType myListItemType;

        public myTemplate()
        {
        }
   
        public myTemplate(ListItemType Item)
        {
            myListItemType = Item;
        }              
    }
    //////////////////////////////////////////////////////////////////////////////////
}

推荐答案

var result = from t in data.SUB_HEAD
             join x in data.HEADs
         on t.head_code equals x.head_code
             Where t.head_code=="HeadCodeSelectedFromGrdiviewDataKey"                        select t;


这篇关于在ADO.NET中以编程方式嵌套网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:17