Click Here

相信能,才能
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

DataGrid嵌套

Posted on 2006-07-13 11:39  鬼手  阅读(377)  评论(0)    收藏  举报
1.Query.aspx
.
<script language="javascript">
        
<!--
        
function showhide(obj,i)
        
{
        
for(j=0;j<i;j++)
        
{
        tr
=document.getElementById("tr"+j);
        
if("tr"+j!=obj)tr.style.display="none";
        
        }

        
        tr
=document.getElementById(obj);
        
        
if(tr.style.display=="none")
        
{
        tr.style.display
="block";
        }

        
else
        
{
        tr.style.display
="none";
        }

        }

        
-->
        
</script>
.
<asp:datagrid id="DgSubsidy" runat="server" AutoGenerateColumns="False" BorderWidth="1" ShowHeader="True"
AllowPaging
="false" Width="600px" CellSpacing="1" BorderStyle="Solid" BorderColor="#eeeeee" OnItemDataBound="DgSubsidy_ItemDataBound">
      
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" Height="30px" BackColor="#F4F4F4"></HeaderStyle>
  
<Columns>
              
<asp:BoundColumn DataField="ID" HeaderText="操作">
                  
<ItemStyle HorizontalAlign="Center" Height="25px" Width="80px"></ItemStyle>
             
</asp:BoundColumn>
              
<asp:BoundColumn DataField="月份" HeaderText="月份">
                  
<ItemStyle HorizontalAlign="Center" Height="25px"></ItemStyle>
              
</asp:BoundColumn>
              
<asp:BoundColumn DataField="岗位津贴" HeaderText="岗位津贴">
                   
<ItemStyle HorizontalAlign="Center" Height="25px"></ItemStyle>
             
</asp:BoundColumn>
              
<asp:BoundColumn DataField="课时酬金" HeaderText="课时酬金">
                   
<ItemStyle HorizontalAlign="Center"></ItemStyle>
              
</asp:BoundColumn>
              
<asp:BoundColumn DataField="扣款" HeaderText="扣款">
                   
<ItemStyle HorizontalAlign="Center"></ItemStyle>
            
</asp:BoundColumn>
            
<asp:BoundColumn DataField="实发金额" HeaderText="实发金额">
                  
<ItemStyle HorizontalAlign="Center"></ItemStyle>
            
</asp:BoundColumn>
             
<asp:TemplateColumn>
            
<ItemStyle BackColor="#EEEEEE"></ItemStyle>
             
<ItemTemplate>
              
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#ffffff">
               
<tr>
                  
<td><asp:datagrid id="Sub1" Runat="server" AutoGenerateColumns="False" BorderWidth="1" BorderStyle="Solid" BorderColor="#006699" ShowHeader="True" Width="100%" OnItemDataBound="Sub_ItemDataBound">
                          
<Columns>
                              
<asp:BoundColumn DataField="岗位津贴" HeaderText="岗位津贴">
                                 
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                              
</asp:BoundColumn>
                              
<asp:BoundColumn DataField="科研津贴" HeaderText="科研津贴">
                                  
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                             
</asp:BoundColumn>
                              
<asp:BoundColumn DataField="取暖降温" HeaderText="取暖/降温费">
                                  
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                              
</asp:BoundColumn>
                              
<asp:BoundColumn DataField="课时酬金" HeaderText="课时酬金">
                                 
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                             
</asp:BoundColumn>
                              
<asp:BoundColumn DataField="年终奖" HeaderText="年终奖">
                                
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                              
</asp:BoundColumn>
                         
</Columns>
                         
<HeaderStyle BackColor="#557691" Font-Bold="true" Height="30" HorizontalAlign="Center" ForeColor="#ffffff"></HeaderStyle>
                 
</asp:datagrid>
                 
</td>
              
</tr>
             
<tr>
                  
<td>
                     
<asp:datagrid id="Sub2" Runat="server" AutoGenerateColumns="False" BorderWidth="1" BorderStyle="Solid"  BorderColor="#006699" ShowHeader="True" Width="100%" OnItemDataBound="Sub_ItemDataBound">
                       
<Columns>
                            
<asp:BoundColumn DataField="加班费" HeaderText="加班费">
                                  
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                            
</asp:BoundColumn>
                            
<asp:BoundColumn DataField="补一" HeaderText="补一">
                                 
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                           
</asp:BoundColumn>
                            
<asp:BoundColumn DataField="扣款" HeaderText="扣款">
                                 
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                           
</asp:BoundColumn>
                            
<asp:BoundColumn DataField="实发金额" HeaderText="实发">
                                 
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                            
</asp:BoundColumn>
                            
<asp:BoundColumn DataField="公积金余额" HeaderText="年初公积金余额">
                                 
<ItemStyle Height="25px" HorizontalAlign="Center" BackColor="#ffffff"></ItemStyle>
                            
</asp:BoundColumn>
                        
</Columns>
                        
<HeaderStyle BackColor="#557691" Font-Bold="true" Height="30" HorizontalAlign="Center" ForeColor="#ffffff"></HeaderStyle>
                
</asp:datagrid>
                                     
</td>
                                   
</tr>
                              
</table>
          
</ItemTemplate>
       
</asp:TemplateColumn>
    
</Columns>
    </asp:datagrid>
2.Query.aspx.cs
......
protected System.Web.UI.WebControls.DataGrid DgSubsidy;
private string rowc="";
private SqlConnection myConnection=new SqlConnection(ConfigurationSettings.AppSettings["DatabaseString"]);
......


private void Page_Load(object sender, System.EventArgs e)
{
  
// 在此处放置用户代码以初始化页面

  
if(!Page.IsPostBack)
   
{
    BindGrid();
   }


}


public void BindGrid()
{
  myConnection.Open();
  DataSet NewsDataSet
=new DataSet();
  
string str="查询语句";
  SqlDataAdapter myAdapter
=new SqlDataAdapter(str,myConnection);
  myAdapter.Fill(NewsDataSet,
"Tea_Subsidy");
DgSubsidy.DataSource 
= NewsDataSet.Tables["Tea_Subsidy"].DefaultView;
  
//返回记录数
  rowc=NewsDataSet.Tables["Tea_Subsidy"].Rows.Count.ToString();
  DgSubsidy.DataKeyField
="ID";
  DgSubsidy.DataBind();
  myConnection.Close();
}


public void DgSubsidy_ItemDataBound(object sender,DataGridItemEventArgs e)
{
 
if(e.Item.ItemType!=ListItemType.Header)
  
{
   
//第0列,ID
   string pstr=e.Item.Cells[0].Text.Trim();

   
//用于格式化小数位
   Check numcheck=new Check();   
   
for(int ii=0;ii<e.Item.Cells.Count-1;ii++)
     
{
      e.Item.Cells[ii].Text
=numcheck.CheckString(e.Item.Cells[ii].Text.Trim());
     }

   
   
//给第0列添加点击事件
      string str="<a href=javascript:showhide('tr"+e.Item.ItemIndex.ToString()+"',"+rowc+")><img src='../Images/p1.gif' border='0'></a>";
      e.Item.Cells[
0].Text=str;
  
//改写第5列(每行变为2行)
      e.Item.Cells[5].Text+="</td></tr><tr id='tr"+e.Item.ItemIndex.ToString()+"' style='display:none;'><td algin='center'>";
      e.Item.Cells[
5].Text+="<a href='PrintSubsidy.aspx?ID="+pstr;
      e.Item.Cells[
5].Text+="' target='_blank'><img src='../Images/p3.gif' border='0'></a>";
      e.Item.Cells[
6].Visible=true;
      e.Item.Cells[
6].ColumnSpan=5;
  }

  
else
  
{//隐藏第6列标题行
   e.Item.Cells[6].Visible=false;
  }

}


public void DgSubsidy_ItemCreated(object sender, DataGridItemEventArgs e)
{
  
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
   
{
    DataSet ds
=new DataSet();
    
string str="SELECT * FROM [table名] where ID="+this.DgSubsidy.DataKeys[e.Item.ItemIndex].ToString();
    SqlDataAdapter myAdapter
=new SqlDataAdapter(str,myConnection);
    myAdapter.Fill(ds,
"Tea_Subsidy1");
    
//绑定两个子表
    DataGrid myDg1=(DataGrid)e.Item.FindControl("Sub1");
    myDg1.DataSource 
= ds.Tables["Tea_Subsidy1"].DefaultView;
    myDg1.DataBind();

    DataGrid myDg2
=(DataGrid)e.Item.FindControl("Sub2");
    myDg2.DataSource 
= ds.Tables["Tea_Subsidy1"].DefaultView;
    myDg2.DataBind();
   }
  
}


public void Sub_ItemDataBound(object sender,DataGridItemEventArgs e)
{
  
if(e.Item.ItemType!=ListItemType.Header)
   
{
    
//格式化子表的小数位
     Check numcheck=new Check();
    
for(int ii=0;ii<e.Item.Cells.Count;ii++)
     
{
      e.Item.Cells[ii].Text
=numcheck.CheckString(e.Item.Cells[ii].Text.Trim());
     }

   }

}


Web 窗体设计器生成的代码