how to add a button in datagridview in vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lucoin
    New Member
    • Sep 2007
    • 24

    how to add a button in datagridview in vb.net

    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
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    Code:
    Dim buttonColumn As New DataGridViewButtonColumn
    buttonColumn.Name = "Button"
    DataGridView1.Columns.Add(buttonColumn)

    Comment

    • lucoin
      New Member
      • Sep 2007
      • 24

      #3
      Originally posted by MrMancunian
      Code:
      Dim buttonColumn As New DataGridViewButtonColumn
      buttonColumn.Name = "Button"
      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.

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        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.DataGridV iewCellEventArg s. It is saved in 'e' as shown in the line of code beneath:

        Code:
        Private Sub DataGridView1_CellClick(ByVal sender As Object, [B]ByVal e As System.Windows.Forms.DataGridViewCellEventArgs[/B]) 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:

        Code:
        Dim gr As New DataGridView
        gr = sender
          Select Case e.ColumnIndex
            Case Is > -1
              Select Case gr.Columns(e.ColumnIndex).Name
                Case "Button"
                  return gr.Rows(e.RowIndex).......etc.

        Comment

        • lucoin
          New Member
          • Sep 2007
          • 24

          #5
          Originally posted by MrMancunian
          Code:
          Private Sub DataGridView1_CellClick(ByVal sender As Object, [B]ByVal e As System.Windows.Forms.DataGridViewCellEventArgs[/B]) 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

          Comment

          • lucoin
            New Member
            • Sep 2007
            • 24

            #6
            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

            Comment

            Working...