今天遇到了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 ;
}
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类{
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 ;
}
}
}
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;
}
}
{
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;
}
}

浙公网安备 33010602011771号