DataGridViewButtonColumn problem

reubenfoo

Member
Joined
Nov 25, 2008
Messages
18
Location
Singapore
Programming Experience
Beginner
Hi all, may i know how do i have the onclick() event for DataGridViewButtonColumn?

I would want to display the row that got selected.anyone has the VB coding?. please help.. thanks

attached is the screenshot of my application.
 

Attachments

  • 11.jpg
    11.jpg
    47.5 KB · Views: 50
hi il try to help you, cause i had same problem some days ago...

Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Dim PMEPMT As Date
Dim PMEDOC As String
Dim PYDATE As Date
Dim PMVNCD As String
Dim VENNAM As String
Dim PMORPT As Integer
PMEPMT = DataGridView1.Rows(e.RowIndex).Cells(0).Value
PMEDOC = DataGridView1.Rows(e.RowIndex).Cells(1).Value
PYDATE = DataGridView1.Rows(e.RowIndex).Cells(2).Value
PMVNCD = DataGridView1.Rows(e.RowIndex).Cells(3).Value
VENNAM = DataGridView1.Rows(e.RowIndex).Cells(4).Value
PMORPT = DataGridView1.Rows(e.RowIndex).Cells(5).Value
Dim form As New 'The form where you will do update to your data in my case Form2
Form.MdiParent = 'indexForm
form.Show()
Form.change(PMEPMT, PMEDOC, PYDATE, PMVNCD, VENNAM, PMORPT)
End Sub

that will be when you doubleclick on one of the rows of datagrid, you will go to form2 to update data of te row you clicked, for it works. :)

"change" is a Private Sub that i use to open the row that i doubleclicked on Form2.

Sub change(ByVal PMEPMT As Date, ByVal PMEDOC As String, ByVal PYDATE As Date ' for all)
PMEPMTTextBox.Text = PMENT '<- = Row of Table of your database
PMEDOCTextBox.Text = PMEDOC '<- =
PYDATETextBox.Text = PYDATE '<- =
.
.
.


i dont know if you will understand smethingwith my crap english.:p

novaimagem2ga2.jpg

novaimagem4mq8.jpg
 
Last edited by a moderator:
Kankichi, why don't you attach images to the post? That way we don't have to wait 2 minutes for the image to load at ImageLack and it won't be gone tomorrow. I attached these two images to your post.
 
hi, i cannot seem to make it work..

the form i am going to edit in is form3. i don't really understand how the code works.

there are alot of errors.. please do help! thanks:D

form2.jpg

form3.jpg
 
Why not allow the datagridview to be editable, change the information on the datagrid and then have an 'Update' button on the form that will update any changed rows(RowState = Modified)? This is assuming you are updating a database (or some sort of storage) behind this. Then you are only dealing with one form.
 
Why not allow the datagridview to be editable, change the information on the datagrid and then have an 'Update' button on the form that will update any changed rows(RowState = Modified)? This is assuming you are updating a database (or some sort of storage) behind this. Then you are only dealing with one form.
sounds your way is much more easy then mine, cause i can't explain very good in english :p
 
The code below will work for any backend as long as you can load it into a DataTable. I have not tried this with any of the DataSet adapters so I am not sure if or how it would work them. This is sort of a 'brute force' method and I am working from an app that I had already created. Take a look at this and see what you think. It may not work into your current project.
VB.NET:
Dim dtData as System.Data.DataTable

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try
dtData = new Datatable

 'fill edit table
            If Not FillData(sqlWork, dtData) Then
                MessageBox.Show("Error trying to fill Grid table.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Try
            End If

 'now set the grid datasource
DataGridView1.Datasource = dtData

'grid should be loaded

Catch Exp As Exception
MessageBox.Show(Exp.Message, "Error", MessageBoxButtons.OK)
End Try
End Sub

Public Sub btnProcess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles  btnProcess.Click

       Try

Dim drCurrentEditRow As DataRow
            'loop through each row in the grid and check for changes
            For RowCnt As Integer = 0 To dtData.Rows.Count - 1

                drCurrentEditRow = dtData.Rows(RowCnt)

                'check for changes
                Select Case drCurrentEditRow.RowState
                    Case DataRowState.Added
                        InsertRecords(drCurrentEditRow)

                    Case DataRowState.Deleted
                        DeleteRecords(drCurrentEditRow)

                    Case DataRowState.Modified
                        UpdateRecords(drCurrentEditRow)

                    Case DataRowState.Unchanged


                End Select


               Next

        Catch exp As System.Exception
        MessageBox.Show(Exp.Message, "Error", MessageBoxButtons.OK)

        End Try
    End Sub


'This is used to fill a datatable from a SQL database.
Public Function FillData(ByVal SQLQueryString As String, ByRef PassDataTable As DataTable) As Boolean
        Dim ReturnBool As Boolean = True

        Using sqlConn As New SqlConnection(ConnectionString)

           Try
                PassDataTable = New DataTable
                Using cmdSearch As New SqlCommand(SQLQueryString, sqlConn)
                    Using daGetData As New SqlDataAdapter(cmdSearch)
                        daGetData.Fill(PassDataTable)
                    End Using
                End Using

            Catch exp As Data.SqlClient.SqlException
                ReturnBool = False
              
            Catch exp As System.Exception
                ReturnBool = False
             End Try
        End Using
        Return ReturnBool
    End Function
 
Back
Top