.Net GridView 系列:应用GridView 嵌套显示主从表

  要实现的效果图如下:
 使用嵌套datagrid实现主从表的显示 - 阳光 - 雕琢时光
说明: 以一个供应商网上对帐的页面为例来解释实现方法;页面刚load的时候只显示主表的内容,
当用户点击主表上单据号的连接之后,就在当前这条记录的下面显示这张单的明细。
在明细表中可以选择当前某项费用的征收方式;对于每张单还有一个"同意"和"不同意"Button。
用户点"同意"之后,要处理以下事情:1.将主表的状态改为"已征收",2.将明细表中的征收方式改为用户所选的征收方式既"帐扣"或者是"现金"。

具体实现:先将放一个DataGrid(命名为dgMaster)到Page上,给dgMaster增加一个摸版列,再在该摸版列上放一个Panel(注意这里好象不能用Div,不知道为什么),在Panel上放一些Label,包括一个HyperLink(绑定主表单据号,明细表通过单据号跟主表关联),将主表中要显示的字段绑定到这些Label上(使用DataBinder.Eval()将主表的字段绑定到Label的Text属性上);再在摸版列上放一个DataGrid(命名为dgDetail),dgDetail用来显示单据明细,并且给dgDetail增加一个摸版列,在摸版列上放一个 RadioButtonList,并给RadioButtonList的Items添加两个值
"帐扣"和"现金";再在dgMaster的摸版列上放一个Panel,在该Panel上放一个TextBox(备注)和两个Button("同意"和"不同意")。

dgMaster中摸版列的效果:
使用嵌套datagrid实现主从表的显示 - 阳光 - 雕琢时光
动态绑定dgDetail(明细表):在dgMaster(主表)的ItemDataBound事件中动态加载明细表的数据,代码如下:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
            {                   
                HyperLink hl = (HyperLink)e.Item.FindControl("HyperLink1");//找到HyperLink
                string sID=hl.Text ;
                string sSQL="select * from tb_detail where c_id='"+sID+"'";
                OleDbCommand olecomm=new OleDbCommand(sSQL,oleconn);
                OleDbDataAdapter oleadapter=new OleDbDataAdapter(olecomm);
                DataSet ds=new DataSet();
                oleadapter.Fill(ds);
                DataGrid tempGrid;
                tempGrid=(DataGrid)e.Item.FindControl("dgDetail");//找到dgDetail
                tempGrid.DataSource=ds.Tables[0].DefaultView;
                tempGrid.DataBind();
                Set_Pay_Method(ds.Tables[0].DefaultView,tempGrid);//设置dgDetail摸版列中 RadioButtonList的值
                tempGrid.Style.Add("display","none");//在页面刚load时先将dgDetail隐藏
                string sName = tempGrid.ClientID;//获得当前这个dgDetail转换为客户端代码后的id
                hl.NavigateUrl = "javascript:showFeeg(\""+sName+"\")";//给HyperLink绑定导航代码
               
               
            }
        }
       

        private void Set_Pay_Method(DataView dv,DataGrid dg)
        {
            for(int i=0;i<dv.Table.Rows.Count;i++)
            {
                TableCell tc=dg.Items[i].Cells[2] ;
                RadioButtonList rbl=(RadioButtonList)tc.FindControl("RadioButtonList1") ;
                switch (dv.Table.Rows[i]["c_pay_method"].ToString() )
                {
                    case "帐扣" :
                    {
                        rbl.Items[0].Selected = true;
                        break;
                    }
                    case "现金" :
                    {
                        rbl.Items[1].Selected = true;
                        break;
                    }
                }
            }
        }
function showFeeg(sName) //该函数为客户端函数,如果当前单据的明细是显示状态就将它隐藏,反之亦然
{
    if (document.getElementById(sName).style.display=="none")
          document.getElementById(sName).style.display="block" ;
    else
         document.getElementById(sName).style.display="none" ;
}
动态生成"同意"(BitOk)和"不同意"(BitNo)Button的onClick事件:首先将BitOk的CommandName设为"agree",BitNo的
CommandName设为"noagree",再在dgMaster的ItemCommand事件中处理这两个命令,代码如下:

 private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (e.CommandName=="agree")
            {
                string sSQL="";
                HyperLink hl=(HyperLink)e.Item.FindControl("HyperLink1");
                string sId=hl.Text;
                DataGrid dg=(DataGrid)e.Item.FindControl("dgDetail");
                for(int i=0;i<dg.Items.Count;i++)
                {
                    string sSort=dg.Items[i].Cells[0].Text;
                    RadioButtonList tempRbl=(RadioButtonList)dg.Items[i].Cells[2].FindControl("RadioButtonList1") ;
                    string sPayMethod="";
                    if (tempRbl.SelectedIndex==0)
                        sPayMethod="帐扣";
                    else if (tempRbl.SelectedIndex==1)
                        sPayMethod="现金";
                    else
                        sPayMethod="";

                    sSQL += "update tb_detail set c_pay_method='"+sPayMethod+"' where c_id='"+sId+"' and c_sort="+sSort+";";
                }
                EnjoyItTextBox tempEtb=(EnjoyItTextBox)e.Item.FindControl("EnjoyItTextBox1");//备注
                string sNote=tempEtb.Text ;
                sSQL += "update tb_master set c_note='"+sNote+"' where c_id='"+sId+"'";
                ExecUpdate(sSQL);//执行
            }
            else if (e.CommandName=="noagree")
            {
                 //省略...
            }
            BindData();//重新绑定
        }
注意:在代码中要取它某个属性的值的那些控件的EnableViewState要设为true。

posted

http://zsl79812sun.blog.163.com/blog/static/1234112752009631103456382/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值