Results 1 to 3 of 3

Thread: Cancel users attempt to exit cell in a Datagrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    69

    Question Cancel users attempt to exit cell in a Datagrid

    Hi,

    hopefully someone can help me with this little problem.
    I have a datagrid where users can edit the values in all cells. I also have a
    database containing valid values for all cells in a row. E.g: in the column "Name" the only valid values are "Mort", "Cocher" and "Dew".
    So what i need to do is prevent the user from exiting a cell if the value he
    has typed doesnt match values in the database.

    Can it be done?

    what i have so far is this:

    VB Code:
    1. Private Sub DataView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
    2.         If loading = True Then
    3.             Exit Sub
    4.         End If
    5.         Dim colName As String = DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name
    6.         Dim rowNo As String = DataGridView1.CurrentCell.RowIndex
    7.         Dim value1 As String = DataGridView1.CurrentCell.EditedFormattedValue
    8.         Dim goodTogo As SQLiteDataReader
    9.  
    10.         Select Case colName
    11.             Case "Timetype"
    12.                 SQLstr = "SELECT name FROM tbltimetype WHERE Name Like '" & value1 & "';"
    13.                 goodToGo= OpenDB(SQLstr, path, mdb).ExecuteReader
    14.                 If goodToGo.Read Then
    15.                      'the value exits in the database, do nothing
    16.                       Exit Sub
    17.                 Else
    18.                       DataGridView1.CancelEdit()
    19.                       'So far so good.. Now how do I maintain focus on this cell??(Not exit it)
    20.  
    21.                 End If
    22.                 goodToGo.Close()
    23.                 closeDB()
    24.             Case 2
    25.  
    26.         End Select
    27.  
    28.     End Sub

    Thanx in advance
    Last edited by Riks; Mar 31st, 2006 at 06:45 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cancel users attempt to exit cell in a Datagrid

    You should put a CoMboBox in that column and have the user select a value from a list. That way you're guaranteed of the value entered being valid. Follow the WinForms FAQ link in my signature. That page has lots of good information on various controls, and the DataGrid inparticular. It will have information on putting a ComboBox in a DataGrid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    69

    Re: Cancel users attempt to exit cell in a Datagrid

    Thanx for the reply. I was hoping i wouldnt have to do that, but ill check it
    out. Thanx

    Riks
    2B || !2B

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width