Hi localhost,
Welcome to use Microsoft Newsgroup Service. Based on your problem
description, you want to add a bttton(in a column) into a DataGrid and the
button only shows on the row which is under edit mode, is my understanding
correct?
If so, here is my suggestion:
As you said that "It is fine to display the new column for every row,, but
not a delete button for the rows that are not being edited", yes, we do can
add a new column with a button and the button only shows when the row is
under edit mode.
We can use a Template column such as(I specify all the columns in aspx
file, not use AutoGenerateColumns, I think this way is better):
<asp:datagrid id=gridTest runat="server" AutoGenerateColumns="False">
<Columns>
................//other columns here
<asp:TemplateColumn>
//let the ItemTemplate empty so that nothing will display when the row is
not in edit mode(also you can put some other thins //such as a label, text
,etc...)
<ItemTemplate> </ItemTemplate>
//put a serverside button in the EditItemTemplate so that the button will
show when the row is being edited
<EditItemTemplate>
<asp:Button ID="delete" Text="delete" Runat="server"
CommandName="deleterow"></asp:Button>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
#notice that the <asp:Button> has a CommandName="deleterow", we must set
this attribute so that we can get the button's click event in the
DataGrid's ItemCommand Event handler. For example: choose the
DataGrid(gridTest)'s ItemCommand handler:
private void gridTest_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName.Equals("deleterow")
{
//add your operation code here
}
}
There are two key points, 1) is that use a Template column, and set its
"EditItemTemplate" with a button, the "ItemTemplate without the button"
2) we should capture the button's click event in the DataGrid's
"ItemCommand" event and determind whether the button is clicked by the
"CommandName" attribute of the
"System.Web.UI.WebControls.DataGridCommandEventArgs e".
Please try out my suggestions. If you have any questions on it, please feel
free to let me know.
BTW localhost, since the notify-mail I sent you always returned fail
message said that the mail address "(e-mail address removed)" is unreachable,
would you please have a check on the mail address? Thus, we can send you
the latest update in time, and here is the weblink where you can change
your email setting of the newsgroup :
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn
Thanks.
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)