Connecting Tech Pros Worldwide Forums | Help | Site Map

how to add a button in datagridview in vb.net

Newbie
 
Join Date: Sep 2007
Location: Sydney
Posts: 23
#1: Sep 15 '08
as asked in the title. how to add button to the datagridview in vb.net. to control the actions such as delete one row.
PS:the button are expected to appear at the end of each row.
PS:I use dataset to specify the datasource of datagridview in code.

MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 283
#2: Sep 15 '08

re: how to add a button in datagridview in vb.net


Expand|Select|Wrap|Line Numbers
  1. Dim buttonColumn As New DataGridViewButtonColumn
  2. buttonColumn.Name = "Button"
  3. DataGridView1.Columns.Add(buttonColumn)
Newbie
 
Join Date: Sep 2007
Location: Sydney
Posts: 23
#3: Sep 16 '08

re: how to add a button in datagridview in vb.net


Quote:

Originally Posted by MrMancunian

Expand|Select|Wrap|Line Numbers
  1. Dim buttonColumn As New DataGridViewButtonColumn
  2. buttonColumn.Name = "Button"
  3. DataGridView1.Columns.Add(buttonColumn)

Thank you. It works. but how to add click event to the button to monitor the changes to a special cell in the certain row.
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 283
#4: Sep 16 '08

re: how to add a button in datagridview in vb.net


Quote:

Originally Posted by lucoin

Thank you. It works. but how to add click event to the button to monitor the changes to a special cell in the certain row.

In my opinion, the best way to do this is using the System.Windows.Forms.DataGridViewCellEventArgs. It is saved in 'e' as shown in the line of code beneath:

Expand|Select|Wrap|Line Numbers
  1. Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
You make sure that you know the name of the column the button is in and then you query the 'e' for the columnindex. After that, you track which row is clicked by using 'e' for the rowindex. Something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim gr As New DataGridView
  2. gr = sender
  3.   Select Case e.ColumnIndex
  4.     Case Is > -1
  5.       Select Case gr.Columns(e.ColumnIndex).Name
  6.         Case "Button"
  7.           return gr.Rows(e.RowIndex).......etc.
  8.  
Newbie
 
Join Date: Sep 2007
Location: Sydney
Posts: 23
#5: Sep 17 '08

re: how to add a button in datagridview in vb.net


Quote:

Originally Posted by MrMancunian

Expand|Select|Wrap|Line Numbers
  1. Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
[/code]

Cool,everything works.
as the same I add imagecolumn to the the datagridview. but how to add pics to the buttom. because in my database I save the photo column as relative path in the program folder. how can I reference the path and review the pics on on the imagesbutton.

thanks a lot
Newbie
 
Join Date: Sep 2007
Location: Sydney
Posts: 23
#6: Sep 17 '08

re: how to add a button in datagridview in vb.net


I fond some codes can add images to the column
just like to set the image cells value through a shared method image.fromfile("<ralative path here>")
hope this helps others
Reply