DataGridView - Linked Tables

Hi,

I have a database comprising of 2 tables. Table 1 contains a foreign key which corresponds to the primary key in table 2. I am displaying Table 1 with a datagridview control and am using the Express Version of VB 2005.

I would like to allow the user to type in a value into the datagridview which may or may not correspond to the primary key in Table 2 and for it react in the following way:-

1) If the value entered exists in Table 2 then all is well, and the user is allowed to continue entering values into the other columns of the datagridview.

[The good news is that I have managed to get this bit to work using the cell validating procedure within the datagridview].

2) If the value entered doesn't exist in Table 2 then a window appears showing a list of possible valid entries from Table 2 to pick from... with the option for the user to add an entry to this list (which is stored in Table 2) for use in Table 1

[Whilst I can get window to appear & allow the user to choose from the list, etc, I can't actually then get the value chosen to appear into the current cell in the datagridview. It just retains the value typed in by the user in the first place. Am using the cell validating procedure to check the value entered and to display the list in this case]

I have an implementation of this in vb6, but am struggling to get this to work in VB 2005. Any help would be much appreciated.

Thanks in Advance, Neil.



Answer this question

DataGridView - Linked Tables

  • Ballinvoher

    It sounds like this statement should do what you need.

    DataGridView1.CurrentCell.Value = "Insert this text."


  • Christian Schiedermeier

    Hi,

    Just to let you all know that I have managed to solve the datagridview problem (as outlined above).

    If you use the assignment statement that John has outlined in the CellValidated Event, as opposed to the CellValidating Event (which I was trying to do) then this works!

    Neil.


  • wakewakeup

    Thanks John,

    I have just tried this but the current cell just retains the value that the user typed in. I suspect that it's not working because I'm trying to change the value of a cell that is currently being validated. The code I'm using is as follows:-

    [The ShowTable2List function displays a list of valid entries from Table 2 and returns the value that the user has chosen. The user can also add a value to this list which is stored in Table 2 for subsequent use].

    Any thoughts as to what else I can try

    Private Sub ClassDataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles ClassDataGridView1.CellValidating

    Dim SQL As String

    If ClassDataGridView1.Rows(e.RowIndex).IsNewRow = False Then

    If ClassDataGridView1.IsCurrentCellDirty Then

    Select Case e.ColumnIndex

    Case 0

    SQL = "SELECT * FROM [Table 2] WHERE [Code] = '" & e.FormattedValue.ToString & "'"

    Snap.Open(SQL, cn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockOptimistic)

    If Snap.EOF Then

    Snap.Close()

    ClassDataGridView1.CurrentCell.Value = ShowTable2List(e.FormattedValue.ToString) '<---- It's this assignment that doesn't appear to work...

    SQL = "SELECT * FROM [Table 2] WHERE [Code] = '" & ClassDataGridView1.CurrentCell.Value & "'"

    Snap.Open(SQL, cn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockOptimistic)

    End If

    e.Cancel = Snap.EOF

    If Not e.Cancel Then ClassDataGridView1.Rows(e.RowIndex).Cells(2).Value = Snap("Desc").Value '<----- This bit works fine....

    Snap.Close()

    End Select

    End If


  • DataGridView - Linked Tables