DataGrid Add single row/column?

L

localhost

When I go into Edit mode on a datagrid, I would like to
add a single column to the edited row and display a
hyperlink. If I use the code below, I get a delete
linkbutton in every row, when I only want it for the row
I am editing. It is fine to display the new column for
every row, but not a delete button for the rows that are
not being edited.

System.Web.UI.WebControls.ButtonColumn dc = new
ButtonColumn();
dc.ButtonType =
System.Web.UI.WebControls.ButtonColumnType.LinkButton;
dc.HeaderText = "Delete";
dc.Text = "[Del]";
dc.CommandName = "DeleteRow";
myGrid.Columns.Add( dc );


Thanks.
 
M

MSFT

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.)
 
S

Steven Cheng[MSFT]

Hi localhost,

Have you had a chance to try out my suggestion or have you resolved your
problem? Please let me know if you need
any help.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top