随笔 - 1330  文章 - 1  评论 - 376  0

 

GridView嵌套GridView,对子GridView及父GridView级联删除的问题

http://blog.csdn.net/amandag/archive/2007/05/31/1633395.aspx

利用GridView显示主细表并添加删除、打开、关闭功能(续)

http://dotnet.aspx.cc/article/f73eeaa9-2bdc-47fd-afd2-59f2fa4897f5/read.aspx

GridView的主从多级嵌套

http://hi.baidu.com/freezesoul/blog/item/4678e6dd90a0c6eb77c63867.html

<!--第一层GridView开始-->
     
<asp:GridView ID="GridView1"   OnRowDataBound="GridView1_RowDataBound"   AutoGenerateColumns=false runat="server">
       
<Columns>
         
<asp:TemplateField>
         
<ItemTemplate> 
            
<%#Container.DataItem.ToString()%>
               
<!--第二层GridView开始-->
                
<asp:GridView ID="GridView2" OnRowDataBound="GridView2_RowDataBound"    AutoGenerateColumns=false runat="server">          
                  
<Columns>
                   
<asp:TemplateField>
                   
<ItemTemplate> 
                       
<%#Container.DataItem.ToString()%>
                       
<!--第三层GridView开始-->
                       
<asp:GridView ID="GridView3"   AutoGenerateColumns=false runat=server>
                            
<Columns>
                              
<asp:TemplateField>
                              
<ItemTemplate> 
                               
<%#Container.DataItem.ToString()%>
                              
</ItemTemplate>
                              
</asp:TemplateField>                    
                            
</Columns>
                         
</asp:GridView>
                       
<!--第三层GridView 结束-->
                       
                    
</ItemTemplate>
                   
</asp:TemplateField>
                  
</Columns>
                 
</asp:GridView>
              
<!--第二层GridView结束-->
              
           
</ItemTemplate>
           
</asp:TemplateField>
          
</Columns>
         
</asp:GridView>
         
<!--第一层GridView结束-->

++++++++++.cs代码++++++++++++++++
RowDataBound与1.x中的ItemDataBound也有一腿!

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
     {
        
this.GridView1.DataSource = Al("GridView1Item:");
        
this.GridView1.DataBind();
     }

    
//DataSource
    protected ArrayList Al(string GetStr)
     {
         ArrayList al 
= new ArrayList();
        
for(int i=0;i<2;i++)
         {
             al.Add(GetStr
+i.ToString());
         }

        
return al;
     }


    
//GridView1_RowDataBound
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
        
if (e.Row.RowType == DataControlRowType.DataRow)
         {
             GridView gv 
= (GridView)e.Row.FindControl("GridView2");
             gv.DataSource 
= Al("GridView2Item:");
             gv.DataBind();
            
         }
     }

    
//GridView2_RowDataBound
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
     {
        
if (e.Row.RowType == DataControlRowType.DataRow)
         {
             GridView gv 
= (GridView)e.Row.FindControl("GridView3");
             gv.DataSource 
= Al("GridView3Item:");
             gv.DataBind();

         }
     }
}



//绑定
    public void bind()
    {
        
string sqlstr = "select * from 表";
        sqlcon 
= new SqlConnection(strCon);
        SqlDataAdapter myda 
= new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds 
= new DataSet();
        sqlcon.Open();
        myda.Fill(myds, 
"");
        GridView1.DataSource 
= myds;
        GridView1.DataKeyNames 
= new string[] { "id" };//主键
        GridView1.DataBind();
        sqlcon.Close();
    }



=========================================

我已经通过FindControl找到这个gridview,可是现在的问题是我要给这个gridview里帮定的数据源是个新的,和外面的gridview数据源不是同一个!下面是我的部分代码: 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"> 
   
<Columns> 
     
<asp:BoundField DataField="type" HeaderText="type" /> 
       
<asp:TemplateField> 
         
<ItemTemplate> 
           
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" EnableViewState="False"> 
             
<Columns> 
                
<asp:BoundField DataField="pub_id" HeaderText="pub_id" /> 
             
</Columns> 
           
</asp:GridView> 
         
</ItemTemplate> 
       
</asp:TemplateField> 
    
</Columns> 
</asp:GridView> 


     
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
         
if (e.Row.RowType == DataControlRowType.DataRow) 
         { 
             e.Row.Cells[
1].FindControl("GridView2"); 
         } 
     } 

可是在FindControl("GridView2")后只有一个DataBind()方法,并没有DataSource这个属性,要怎样才能给里面的那个GridView2指定一个新的数据源呢?


--------------------------------------------------------

GridView GRTemp=(GridView)e.Row.Cells[1].FindControl("GridView2");
GRTemp.DataSource=你的source;
GRTemp.DataBind();

========================================================================================

Repeater嵌套:

=======================================================================================

//在绑定分类品名时,绑定分类下的产品 
private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) 

    BLL.Products products 
=new BLL.Products(); 
    
if (e.Item.ItemType == ListItemType.Item ||    e.Item.ItemType == ListItemType.AlternatingItem)  
    { 
        Repeater rptProduct 
= (Repeater) e.Item.FindControl("rptProduct"); 
        
//找到分类Repeater关联的数据项 
        DataRowView rowv = (DataRowView)e.Item.DataItem; 
        
//提取分类ID 
        int CategorieId = Convert.ToInt32(rowv["ID"]); 
        
//根据分类ID查询该分类下的产品,并绑定产品Repeater 
        rptProduct.DataSource = products.GetProductsByCategorieId(CategorieId); 
        rptProduct.DataBind(); 
    } 

=========================================================================================

gridView嵌套:

========================================================================================

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

        {
            DataRowView rowv 
= (DataRowView)e.Row.DataItem;
          
            Label lblCareer 
= (Label)e.Row.FindControl("lblCareer");
            
string tmp_myCareer = rowv["mycareer"].ToString().Trim();
           

          }
     }


 

posted on 2009-08-17 13:54 钱途无梁 阅读(...) 评论(...) 编辑 收藏