问题
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.
回答1:
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
回答2:
<asp:buttonfield ButtonType="Image" ImageUrl="/images/edit.gif" commandname="ibtnEdit" HeaderText=" " />
回答3:
This Code Project article looks like it's exactly what you need.
回答4:
On event dataGridViewMain_CellContentClick write following code
if (e.ColumnIndex == dataGridViewMain.Columns["ImageColumn"].Index)
{
lblShowCellData.Text = dataGridViewMain.Rows[e.RowIndex].Cells["CustomerName"].Value.ToString();
// Do some thing else....
}
download full source code at http://tablegridview.blogspot.in
回答5:
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>
来源:https://stackoverflow.com/questions/2673938/add-imagebutton-to-datagridview-in-c-sharp