tags:

views:

308

answers:

4

I am trying to do a simple 'Delete Row' button in a DataGridView that I have, but the problem is that I would like it to be an ImageButton rather than a simple Button.

Currently I have it setup to be a ButtonColumn, but have seen no possibility for changing it from a simple button with text to a button with an image on it.

I know this is possible and hopefully relatively simple, but am unsure of how to go about doing it.

EDIT

Sorry, I should clarify. This is a standalone app in straight C#...I don't have access to the ASP form types.

A: 
<asp:buttonfield ButtonType="Image" ImageUrl="/images/edit.gif"  commandname="ibtnEdit"  HeaderText=" " />
John Hpa
please see edit
espais
Opps! I think it's asp.net. Sorry.
John Hpa
A: 

You can use a Template Field in GridView. Just like this:

<asp:TemplateField HeaderText="Delete">
  <ItemTemplate>
    <asp:ImageButton ID="imgBtnDelete" runat="server" OnClick="imgBtnDelete_Click" ImageUrl="~/media/Delete.gif">
    </asp:ImageButton>
  </ItemTemplate>
</asp:TemplateField>
Gunner 4 Life
please see edit
espais
+1  A: 

If you're willing to just go for a clickable image as opposed to an ImageButton, there's a pretty simple solution.

Add a ImageColumn to your dgView, set its "NullValue" to be a red X or whatever you want it to be. This will ensure that all of the rows will always have the X showing with no extra work.

After that, you're going to want to add a "CellContentClick" event to capture the user actually clicking on the X.

Inside the cellcontent click event, you can check if

//check if your clicking on a cell inside the imagecolumn column
if(e.ColumnIndex == this.colImageColumn.index && e.RowIndex > 0)
   //Delete Row e.RowIndex
greggorob64
+1  A: 

This Code Project article looks like it's exactly what you need.

Robb