הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, August 7, 2014 2:00 PM
Hi
Does anyone know how to programme up a ?Delete Image Button? using an Icon for a DataGridView in Visual Studio .Net
I am struggling find trying to get my last column in my DataGridview to have a delete icon instead of the a "Delete Button" and I want to display a little dustbin icon.
I have tried looking exhaustively in many forums and can find #c examples but not .NET. I am unable to get this to work.
Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)
If e.ColumnIndex = 1 AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
Dim img As Image = Image.FromFile("C:\icon_delete.jpg")
e.Graphics.DrawImage(img, e.CellBounds.Left + 10, e.CellBounds.Top + 5, 10, 10)
e.Handled = True
End If
End Sub
Would appreciate some help if anyone knows. Thanks in Advance. Kuldip.
Kuldip Mond
All replies (11)
Monday, August 11, 2014 2:11 PM ✅Answered | 4 votes
Hi,
You can try this. I just made sure the button column has a name so it can be identified in the Cell_Paint event instead of using an index number to identify it. I also made sure the cell has no text added to it when a row is added to the DataGridView. I also made it draw the image centered in the button cells. If your image is larger than the cells then the DrawImage method will also need to have a Height and Width specified to draw the image small enough to fit in the cell.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bc As New DataGridViewButtonColumn
bc.Tag = False
bc.Text = "Delete"
bc.Name = "Delete"
bc.Width = 43
DataGridView1.Columns.Add(bc)
'I just used this to add a few rows for this example
For x As Integer = 0 To 4
DataGridView1.Rows.Add("Some Info", "Other Stuff", "More...", "") 'Added blank string as the button columns text value
Next
End Sub
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If DataGridView1.Columns(e.ColumnIndex).Name = "Delete" AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
e.Graphics.DrawImage(My.Resources.Delete, CInt((e.CellBounds.Width / 2) - (My.Resources.Delete.Width / 2)) + e.CellBounds.X, CInt((e.CellBounds.Height / 2) - (My.Resources.Delete.Height / 2)) + e.CellBounds.Y)
e.Handled = True
End If
End Sub
End Class
If you say it can`t be done then i`ll try it
Friday, August 8, 2014 3:05 AM | 1 vote
Hi,
Because you are developing with Visual Basic, I moved this thread to Visual Basic forum for better support.
Thanks,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Friday, August 8, 2014 9:16 AM
Maybe you mean this one http://social.msdn.microsoft.com/forums/vstudio/en-US/f7f4fa33-5aed-4074-a4cd-ebf43dd8362c/image-button-in-datagridview
But what did you mean by not work? Couldnot show that image? Or could not click that button?
What is the type of that column? DataGridViewButtonColumn, DataGridViewImageColumn or the other?
remember make the reply as answer and vote the reply as helpful if it helps.
Friday, August 8, 2014 10:30 AM
Hello,
Here is a working example I did in VS2010, VB.NET.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Sunday, August 10, 2014 7:33 PM
Hi Sorry for not saying.
The image of the delete symbol/icon is not being displayed.
i.e.
1) I have tried the example given above :
Does not compile or understand the "+=" in Visual Studio Professional at the following two lines.
DataGridView1.CellClick += New DataGridViewCellEventHandler(AddressOf dataGridView1_CellClick)
DataGridView1.CellPainting += New DataGridViewCellPaintingEventHandler(AddressOf dataGridView1_CellPainting)
Saying I need to Raise an Event.
2) I have also tried the working example above but this is done in C# and as such paints a button only upon a cell click event. I need it at the tikme of Form Load.
In summary I am still unable to create my delete button. My code is :
Dim buttonColumnP As New DataGridViewButtonColumn() ' Add a button column to DataGridView 1
buttonColumnP.HeaderText = "Delete"
buttonColumnP.Name = "Delete"
buttonColumnP.Text = "Delete"
buttonColumnP.UseColumnTextForButtonValue = True
DataGridView1.Columns.Add(buttonColumnP)
DataGridView1.Columns(7).Width = 43
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.ColumnIndex = -1 Or e.RowIndex = -1 Then Exit Sub
If DataGridView1.Columns(e.ColumnIndex).Name = "delete" Then
e.Value = My.Resources.delete
End If
End Sub
Any help would be greatly appreciated. Thanks in Advance.
Kuldip Mond
Sunday, August 10, 2014 8:51 PM | 1 vote
Hello,
I am unclear if you looked at my project which as is paints a check-mark on click with an X for the text. By modifying the code a little bit you would have what you want.
This code has been slightly changed so no X appears and a trashcan image is shown on each row. Note the row height is tall because I only had a 24/24 image of a trashcan.
All I did was change the image and commented out the code shown below
Public Class Form1
WithEvents bsCustomers As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bsCustomers.DataSource = _
(
From customer In MockedData()...<Customer>
Select New With
{
.Identifier = customer.<CustomerID>.Value,
.Name = customer.<CompanyName>.Value,
.Address = customer.<Address>.Value,
.City = customer.<City>.Value,
.Country = customer.<Country>.Value
}
).ToList
DataGridView1.DataSource = bsCustomers
Dim bc As New DataGridViewButtonColumn
bc.Tag = False
DataGridView1.Columns.Insert(0, bc)
ActiveControl = DataGridView1
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.ColumnIndex = 0 Then
e.Value = ""
e.FormattingApplied = True
End If
End Sub
Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
Dim bc As DataGridViewButtonCell = TryCast(DataGridView1(0, e.RowIndex), DataGridViewButtonCell)
'Dim x As Boolean = True
'If bc.Tag Is Nothing Then
' x = False
'Else
' x = CBool(bc.Tag)
'End If
Dim bm As New Bitmap(My.Resources.trashb24)
e.Graphics.DrawImage(bm, (e.CellBounds.Width - 10), e.CellBounds.Top + 10)
'If x Then
' Dim bm As New Bitmap(My.Resources._0090_check)
' e.Graphics.DrawImage(bm, e.CellBounds.Left + 3, e.CellBounds.Top + 3)
'End If
e.Handled = True
End If
End Sub
Private Sub dataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
Dim bc As DataGridViewButtonCell = TryCast(DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewButtonCell)
If bc.Tag Is Nothing Then
bc.Tag = True
Else
bc.Tag = Not CBool(bc.Tag)
End If
End If
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Monday, August 11, 2014 7:20 AM
Great, I think I am nearly there. I can see what you mean now. Sorry.
Ok, after getting this coded up and working, I tried changing to work on the 7th Column:
Dim bc As New DataGridViewButtonColumn
bc.Tag = False
DataGridView1.Columns.Insert(7, bc)
DataGridView1.Columns(7).Width = 30
and everywhere I saw a columnIndex association (e.ColumnIndex = 0, I replaced with e.ColumnIndex = 7). i.e.
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.ColumnIndex = 7 Then
e.Value = ""
e.FormattingApplied = True
End If
End Sub
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.ColumnIndex = 7 AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
Dim bc As DataGridViewButtonCell = TryCast(DataGridView1(7, e.RowIndex), DataGridViewButtonCell)
Dim bm As New Bitmap(My.Resources.delete)
e.Graphics.DrawImage(bm, (e.CellBounds.Width - 20), e.CellBounds.Top + 2)
e.Handled = True
End If
End Sub
I guess I am failing to see what anchors the image to the column and didn't work.
I thought this was going to be a breeze now, but the button still appears at the first column ?
ps. What I really wanted was to have no button at all or a transparent but this is a bit more complex than meets the eye, as I have read from how to create a datagridview with transparent background so I might tackle that after.
Thanks in advance. K
Kuldip Mond
Monday, August 11, 2014 12:08 PM
Hello,
A bit more code is needed for doing the image in another location, don't have code for it but if I needed too the following is what I would use.
Code Project article which is in C# but should be easy to use a converter to VB.NET and would place each of the classes in the form in a class project.
One reason this requires a good deal of code in my opinion is there really is no call for a button with an image, otherwise Microsoft would had added a property for doing an image.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Monday, August 11, 2014 1:33 PM
Hi,
Indeed I am aware of the Code Project article in C# as mentioned but failed to yield any useable code that worked in VB.NET.
Interesting, I do not actually want a button with an image superimposed on it. I simply need a icon/dustbin image in the DataGridView Column without the button.
I am reasonably confident that I have seen applications where delete icons are in the right most column. Often like the following :
at the end of a row.
Regards Kuldip.
Kuldip Mond
Monday, August 11, 2014 7:36 PM
Absolutely, fantastic. Works perfectly. Double point for that one. A humble thank you for your efforts.
Kuldip
Kuldip Mond
Monday, August 11, 2014 8:44 PM | 1 vote
Absolutely, fantastic. Works perfectly. Double point for that one. A humble thank you for your efforts.
Kuldip
Kuldip Mond
Your Welcome. Glad it helped. :)
If you say it can`t be done then i`ll try it