今天遇到了datagrid中动态添加模板列的问题

要求对datagrid中的一列,某些有权限的人员可以点击该列编辑修改,而对普通浏览人员不产生该列。
定义、添加模板列
            addNew.Visible  =   false  ;
            string strsql 
= " select count(*) from zhibankuaibaoconfig where value=' " +  curUsrName  + " ' and typeid = " ;
            
int  count  =  ( int )SqlHelper.ExecuteScalar(CommandType.Text,strsql + " 0 " );
            
if (count  >   0  )
            {
                TemplateColumn tempColumn 
=   new  TemplateColumn();
                tempColumn.HeaderText  
=   " 修改 "  ;
                string txtContent 
=   " <img alt=\ " 修改\ "   border=\ " 0 \ "  src=\ " img\\edit.gif\ " > "  ;
                tempColumn.ItemTemplate
=   new  ColumnTemplate(txtContent);
                DataGrid1.Columns.Add(tempColumn);
                addNew.Visible 
=   true  ;
            }


添加的模板列的单击事件
        private  void  DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            string curUsrName 
=  mUserInfo.FullName;
            
if  ( e.Item.ItemType  ==  ListItemType.Item  ||  e.Item.ItemType  ==  ListItemType.AlternatingItem )
           {
                e.Item.Attributes.Add(
" onmouseover " " preStyle=this.style.backgroundColor ; this.style.backgroundColor='Bisque' " );
            e.Item.Attributes.Add(
" onmouseout " "  this.style.backgroundColor=preStyle  "  );
            e.Item.Style[
" cursor " =   " hand " ;
                string strsql 
= " select count(*) from zhibankuaibaoconfig where value=' " +  curUsrName  + " ' and typeid = " ;
                
int  count  =  ( int )SqlHelper.ExecuteScalar(CommandType.Text,strsql + " 0 " );
                
if (count  >   0  )
                {
                    e.Item.Cells[
5 ].Attributes.Add( " onclick " " go(\ " IndexInfoView.aspx ? dateid_M = "  + e.Item.Cells[0].Text +  " \ " ,1) " );
                    addNew.Visible 
=   true  ;
                }
            }
        }
上边用到的ColumnTemplate类
public class ColumnTemplate : ITemplate
{
    private string txtContent;
    public  ColumnTemplate(string txt)
    {
        txtContent 
=  txt ;
    }
    public 
void  InstantiateIn(Control container)
    {
        LiteralControl lc
= new  LiteralControl();
        lc.DataBinding
+= new  EventHandler( this .OnDataBinding);
        container.Controls.Add(lc);
    }
    public 
void  OnDataBinding(object sender,EventArgs e)
    {
        LiteralControl lc
= (LiteralControl) sender;
        DataGridItem  container1
= (DataGridItem) lc.NamingContainer;
        lc.Text
=  txtContent;
    }
}


转载于:https://www.cnblogs.com/mapk/archive/2007/07/04/806307.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值