Results 1 to 6 of 6

Thread: [RESOLVED] DataGridView WinForms

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Resolved [RESOLVED] DataGridView WinForms

    I have a WinForms app that has a DGV that's bound to a BindingSource and when the user clicks on the new row line @ the bottom I would like it to fill in two columns values automatically but I'm not sure which event is fired when that row is switches to user editing mode.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataGridView WinForms

    try this:

    vb Code:
    1. Private Sub dgv1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv1.CellMouseDown
    2.     If e.RowIndex = dgv1.NewRowIndex Then
    3.         dgv1(0, e.RowIndex).Value = "test1"
    4.         dgv1(1, e.RowIndex).Value = "test2"
    5.     End If
    6. End Sub

  3. #3

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] DataGridView WinForms

    Thanks, I changed it from MouseDown to MouseUp and all's working right.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [RESOLVED] DataGridView WinForms

    why? MouseDown worked ok for me

  5. #5

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] DataGridView WinForms

    Because I would have to click twice to get it to show when I was using the MouseDown event. In the Click event internally it sets the cells when it's moved to the new index, so by using the MouseUp event my data is added and it sticks on the first click.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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

    Re: [RESOLVED] DataGridView WinForms

    This should have nothing at all to do with the grid. Does it not seem like a hack handling mouse events of a grid to populate fields in new bound items?

    You should be handling the AddingNew event of your BindingSource. There you can either create an populate a new item or else set the fields of the new DataRowView, depending on what you're bound to.

    The whole point of using a BindingSource is so that you can do data-binding related things like this easily. If you don't use the BindingSource for this sort of thing then using it at all is pointless.
    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

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