Thanks for Bill's informative suggestions.
Hi Alphonse,
From the your description, you'd like to dynamically hidden a certain
button column in a webform datagrid according to a certain columns's data
of the DataGrid's datasroure. And curretnly you did that in the
ItemDataBound event, however you found there occured some problems that
when hiting edit or update, the hidden column's button occured again, yes?
I've viewed Bill's message and feel that you can have a check on those
suggestions. Also, below are some of mine suggestions:
1. Have you added the code that check the Item's ItemType in the DataGrid's
ItemDataBound event? such as
private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
{
// your code here
}
}
Since you'd like to hidden the certain button all the time no matter it is
under editing or not, you'd like to include all the three types "Item",
"AlternatingItem", "EditItem"
2. Also, since the "Select", "Edit/Update/Cancel" and "Delete" button
columns are buildin button columns of webform datagrid. We can also use
TemplateColumn to define a custom button column as we like, just add a
button and set it's commandname propery ...
Thus, It'll be much easier to control the button's visible, do you think so?
In addition, since I'm not quite sure on your detailed code logic, I think
it'll be helpful if you can provide some further code snipets on your code.
And below is a demo page I used I my side, please refer to it to see
whether it helps.
-----------------------aspx page----------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DemoGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp

ataGrid id="dgMain" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:ButtonColumn Text="Select"
CommandName="Select"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="deleteable"
HeaderText="Deleteable"></asp:BoundColumn>
</Columns>
</asp

ataGrid>
</td>
</tr>
</table>
</form>
</body>
</HTML>
---------------------code behind page class----------------------
public class DemoGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgMain;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("deleteable",typeof(bool));
DataRow dr = null;
bool[] flags = {true,false};
for(int i=1;i<=10;++i)
{
dr = tb.NewRow();
dr["index"] = i.ToString();
dr["name"] = "Name" + i.ToString();
dr["deleteable"] = flags[i%2];
tb.Rows.Add(dr);
}
dgMain.DataSource = tb;
dgMain.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.dgMain.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_CancelComm
and);
this.dgMain.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_EditComman
d);
this.dgMain.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_UpdateComm
and);
this.dgMain.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMain_ItemDataBound
);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgMain_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = -1;
BindGrid();
}
private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
bool deleteable = (bool)drv["deleteable"];
if(!deleteable)
{
e.Item.Cells[2].Controls[0].Visible = false;
}
else
{
e.Item.Cells[2].Controls[0].Visible = true;
}
}
}
private void dgMain_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = -1;
BindGrid();
}
private void dgMain_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
}
-------------------------------------------------------------------------- --
------------
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx