Question Datagrid cell value change

kimcodz

New member
Joined
Mar 9, 2013
Messages
3
Programming Experience
1-3
Hi guys, all I wanted to do is to change the value of cell B, automatically, whenever I'll change the value of cell A. That's all. The datagrid is not bounded to DB. Just a simple datagird with simple data in it. I hope you can help me... I am having a hard time doing this... Thanks a lot.
 
Hi,

When you say DataGrid I assume that you mean a DataGridView? If so then you can code the CellValueChanged event, checking for a change to the first column A, index 0, and then assign a value to the second column B, index 1. Have a look at this example:-

VB.NET:
Private Sub CustomersDataGridView_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CustomersDataGridView.CellValueChanged
  If CustomersDataGridView.CurrentCell.ColumnIndex = 0 Then
    CustomersDataGridView.CurrentRow.Cells(1).Value = "New Value"
  End If
End Sub

With this event coded, you can often get issues when initially loading information into the DataGridView since loading rows, for example when a form loads, with cause the event to fire. If you have this issue you can Remove the Handles clause in the code above and then add the Handler to the control when the Form completes loading in the Form's Shown event. i.e:-

VB.NET:
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
  AddHandler CustomersDataGridView.CellValueChanged, AddressOf CustomersDataGridView_CellValueChanged
End Sub

Hope that helps.

Cheers,

Ian
 
Back
Top