Problem with DataGridView event

syntia.wijaya

Member
Joined
Apr 22, 2007
Messages
24
Location
Indonesia
Programming Experience
Beginner
Hi,
I have a problem with event. I have a DataGridView which one of the column is contains of checkboxes (I use DataGridViewCheckBoxColumn).
When the check box is checked, I want to take the rest of value in the checked row, then put it in the provided text box.

This is my code:
VB.NET:
Private Sub ProjectDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ProjectDataGridView.CellValueChanged
    If ProjectDataGridView.CurrentRow.Cells(0).Value Then
        ProjectIDTextBox.Text = ProjectDataGridView.CurrentRow.Cells(1).Value
        ProjectNameTextBox.Text = ProjectDataGridView.CurrentRow.Cells(2).Value
    End If
End Sub

The problem is, it's always one click late :( Example, when I checked the first row, the value in the text box didnt change. Then I check the second row, the value in the text box changed to the first row value.

Can anyone please help me?

Thank you..
 
How many checkboxes do you have in your Datagridview? If 2. Cause from here it seems what you are trying to do is when the first checkbox is set, it sets the value in the textbox to the information in the second checkbox and almost immediately resets the value in the textbox to the value in the 3rd checkbox. basically what you get is just the value in the third checkbox when you click the first checkbox..... is that what you are trying to do?

Reaction
 
You wouldnt really bother with doing this. Instead you would have the dataridview and the textboxes bound through the same bindingsource, to the data. Then when you change the current row of the datagridview, the contents of the text boxes will change. See the attached project
 

Attachments

  • DataGridAsBindingNav.zip
    20.3 KB · Views: 34
Yet, I have another problem with datagridview CellEndEdit event.

In dgv, there is an empty row as the last row. I have several columns which is spose to be updated by programs, users are not allowed. Those columns are createdBy, createdDate, updatedBy and updatedDate.

When I edit the cell the updatedBy and UpdatedDate column supposed to be updated. But when I entered a new record in the last row, all those 4 columns supposed to be updated. This is where I got the problem. The createdBy and createdDate are not updated at all.

This is my code for the CellEndEdit event:
VB.NET:
Protected Overrides Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal data As DataGridViewCellEventArgs)
    If DataGridView1.Rows(data.RowIndex).IsNewRow Then
        ' Created
        ' Cells(4) is CreatedBy Column
        ' Cells(5) is CreatedDate Column
        Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(4).Value = username
        Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(5).Value = Date.Now()
    End If

    ' Updated
    ' Cells(6) is UpdatedBy Column
    ' Cells(7) is UpdatedDate Column
    Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(6).Value = username
    Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(7).Value = Date.Now()    
End Sub

Thanks in advance..
 
Last edited:
I reckon you really need to stop thinking in the old ways of VB6 where the grid itself contains the data. In modern .NET, the grid doesnt contain the data, it's just a tool for sorting, showing and editing it. Given that the data goes in and out of a container, you should have the container do things like this. Attach some code to one of the Row events of the DataTable upon which the grid is bound
 
Dear cjard,

Actually this is my first time developing application using vb, so I'm not familiar with vb6 at all, never even see the tool.

Do you mean I have to put the code in "sub blah3x (params) handles MyDataTable.Events" ?? So what is the dgv RowValidating event used for? This dgv's really killing me.. :(
 
I've read a glance about MVC architecture before, it's to prevent my code messing up between the GUI, busines logic and db access.

Not really; its much more fundamental than that. However, your code is essentially messing about with a GUI control of a model of data which is somewhere else

To use an analogy, suppose you have a button that when clicked, runs a sub called Foo(). What youre now trying to do is use a timer to pop the window to the front and programmatically move the mouse cursor over the button and issue a click... When you should jsut call Foo() from the timer event
 
Back
Top