Hide Delete Button in DataGrid

A

Alphonse Giambrone

I have a editable datagrid with 6 columns in a user control.
Col 1: linkbutton to select row
Col 2: linkbutton to edit row (changes to update, cancel on edit)
Col 3: linkbutton to delete row
Col 4, 5: visible data columns
Col 6: hidden data column

I want to hide the delete button in col 3 based on the value of the data in
hidden col 6.
In the ItemDataBound event I use 'findcontrol' to find the label containing
the data in col 6.
If it contains the appropriate value, I 'findcontrol' for the delete button
and set its visible property to false.
This works when first loading the page.
Once I edit and save (or cancel) the row the delete button is visible.

I stepped through the code and in the ItemDataBound event after an update
(or cancel ) and the row that was in edit is not 'hit'. Yet it displays.

Any ideas on what is happening and how to hide the button after an
update/cancel?
Or to delete the button, or even not create it in the first place?

TIA
 
G

Guest

There are a few different variations of what you're trying to do, but I like your approach and have done something very similar (I esp. like using findcontrol, whereas a lot of the tutorials seem to want you to use cell indexes, which is begging for trouble)

My best guess is that you are not redoing the DataBind until the subsequent PageLoad, when you should be doing it again in each EditCommand, UpdateCommand, and CancelCommand, or better yet only once for the whole page in the PagePrerender (order of events: PageLoad->control events->PagePrerender)

Another guess is that you are doing the DataBind, but at the point you're doing it the datasource is empty (i.e. an empty variable is pointing to it, which I think rather than give you an error on DataBind just leaves the old items collection out there in the grid)

Are either of these any help? Else, if you don't get anything better from anybody else, maybe you can post a little code

Cheers

Bil

----- Alphonse Giambrone wrote: ----

I have a editable datagrid with 6 columns in a user control
Col 1: linkbutton to select ro
Col 2: linkbutton to edit row (changes to update, cancel on edit
Col 3: linkbutton to delete ro
Col 4, 5: visible data column
Col 6: hidden data colum

I want to hide the delete button in col 3 based on the value of the data i
hidden col 6
In the ItemDataBound event I use 'findcontrol' to find the label containin
the data in col 6
If it contains the appropriate value, I 'findcontrol' for the delete butto
and set its visible property to false
This works when first loading the page
Once I edit and save (or cancel) the row the delete button is visible

I stepped through the code and in the ItemDataBound event after an updat
(or cancel ) and the row that was in edit is not 'hit'. Yet it displays

Any ideas on what is happening and how to hide the button after a
update/cancel
Or to delete the button, or even not create it in the first place

TI
 
S

Steven Cheng[MSFT]

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:DataGrid 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:DataGrid>
</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
 
A

Alphonse Giambrone

Thanks both for your excellent suggestions and comments.
After a night's sleep and fresh jolt of caffeine, I found the problem.
Before 'finding' the button I was testing the type of item so as not to do
the find if not necessary.

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
....find the button.

I forgot that the row in question is the selected row, so what I really
needed was:
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem OrElse e.Item.ItemType =
ListItemType.SelectedItem Then
....find the button

I don't need to do it in edit mode since I am hiding the whole column at
that point.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


Steven Cheng said:
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:DataGrid 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:DataGrid>
</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
 
G

Guest

Excellent. I've gotten stuck more than once forgetting to check for the right itemtype. I've not seen Whidbey, but I'm guessing that this is something they do differently next time around (all I've heard is that the new DataView will make you forget all about DataGrid/List. At the least, there should be an itemtype that's just "item", with another level below that in cases where you need to look at alternating, etc. Come to think of it, I guess it wouldn't be too tough to create my own AnyItem constant, which is or'ed together with item|alternating|selected. At least in my case, it's not very often that I don't want to treat these three together

----- Alphonse Giambrone wrote: ----

Thanks both for your excellent suggestions and comments
After a night's sleep and fresh jolt of caffeine, I found the problem
Before 'finding' the button I was testing the type of item so as not to d
the find if not necessary

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType
ListItemType.AlternatingItem The
....find the button

I forgot that the row in question is the selected row, so what I reall
needed was
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType
ListItemType.AlternatingItem OrElse e.Item.ItemType
ListItemType.SelectedItem The
....find the butto

I don't need to do it in edit mode since I am hiding the whole column a
that point

--

Alphonse Giambron
Email: a-giam at customdatasolutions dot u


Thanks for Bill's informative suggestions
Hi Alphonse
From the your description, you'd like to dynamically hidden a certai
button column in a webform datagrid according to a certain columns's dat
of the DataGrid's datasroure. And curretnly you did that in th
ItemDataBound event, however you found there occured some problems tha
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 thos
suggestions. Also, below are some of mine suggestions
1. Have you added the code that check the Item's ItemType in th DataGrid'
ItemDataBound event? such a
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 her

Since you'd like to hidden the certain button all the time no matter it i
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" butto
columns are buildin button columns of webform datagrid. We can also us
TemplateColumn to define a custom button column as we like, just add
button and set it's commandname propery ..
Thus, It'll be much easier to control the button's visible, do you thin so
In addition, since I'm not quite sure on your detailed code logic, I thin
it'll be helpful if you can provide some further code snipets on you code
And below is a demo page I used I my side, please refer to it to se
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:DataGrid 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:DataGrid>></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();
}
}
-------------------------------------------------------------------------- --(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top