Results 1 to 13 of 13

Thread: [2008] Datagridview row issue

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    [2008] Datagridview row issue

    Hi Guys,

    I have a dgv and I want to be able to add,edit and delete. I've got my code for updating to work. I grab the Id from colum1 of the row to update and write my updatecommand accordingly.

    On the currentcellchange event I am grabbing the Id.

    But when I am adding a new row this event is fired and bcos there is no Id in there an error is trigerred. How can I fix this. On which event should I grab the Id

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Datagridview row issue

    I guess you could implement a system that checks a boolean value to see if you are adding a new row or editing an existing one and then before you clal the code to add a new row you just set this to True. Then if the boolean value is true you dont grab the ID and if its false then you do. Obviously you would need to reset this back to false after a new row is added to be ready for a cell change event.
    However, I'm sure there will be a tidier/better way of doing it that someone with more experience will explain to you
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: [2008] Datagridview row issue

    Use the BindingSource, not the grid.
    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

  4. #4

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview row issue

    please show me how jm

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

    Re: [2008] Datagridview row issue

    Have you read the documentation for the BindingSource class? What events does it provide that might be useful to you? What other members does it provide that might be useful to you?
    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

  6. #6

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview row issue

    ok, i've added a bindingsource and linked it to my dgv

    Code:
    dgvCalls.DataSource = CallBindingSource 
    CallBindingSource.DataSource = calltable
    How should I handle the next step?

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

    Re: [2008] Datagridview row issue

    What occurrences do you want to be notified of?
    I want to be able to add,edit and delete.
    What events does the BindingSource have that would provide notification of those occurrences?
    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

  8. #8

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview row issue

    ok i understand so far , I know i'm being a pest, but I'm a bit swamped with work right now. I have been doing some research tho. I would love it if you could show me the usage of the addnew for example and where I should place my code. sory

  9. #9

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview row issue

    guys please help. i'm stuck

  10. #10
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: [2008] Datagridview row issue

    I may get looked down upon for saying this, but this is how I would do it.

    The first thing you're going to need is a For/Next loop (so that you can loop through each row). I would set up a simple Select statement to check for the record. If the record already exists, set the editing boolean to true, then use your update statement for that particular row. Remember to set the booleans back to false before going to the next row. Here's a quick and dirty example.

    Code:
    dim blnAdd as Boolean
    dim blnEdit as Boolean
    
    For each row in dgvRows
    'Place Select statement here
    'if the query returned a row then
    blnEdit = True
    'Use update query here
    Else 'The query returned 0 rows
    blnAdd = True
    'Use Add query here
    End if
    blnAdd=False
    blnEdit = False
    Next
    For added fun, you can add a blnDelete and make one nice For/Next loop for all your datagridview needs.
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  11. #11

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview row issue

    thanks for the suggestion Blakk_majik,

    but i finally think I am getting somewhere. JMCilhinney, please assist me.

    I figured out how to add event handlers for the Bindingsource.

    I want the following scenario. when editing an exixting row, I want to pull the callid of the current row. so when I am updating I will use that callid for my update commands.

    Code:
    Private Sub CallBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs) _
         Handles CallBindingSource.CurrentChanged
    
            MessageBox.Show("You are in the BindingSource.CurrentChanged event.")
    
        End Sub
    
        Private Sub CallBindingSource_AddingNew(ByVal sender As Object, ByVal e As EventArgs) _
        Handles CallBindingSource.AddingNew
    
            MessageBox.Show("You are in the BindingSource.Addingnew event.")
    
        End Sub
        Private Sub CallBindingSource_PositionChanged(ByVal sender As Object, ByVal e As EventArgs) _
        Handles CallBindingSource.PositionChanged
    
            MessageBox.Show("You are in the BindingSource.PositionChanged event.")
    
        End Sub

  12. #12

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview row issue

    also I am using the class from the smdn website to show datepickers in a column. But when I am adding a new row I want todays date to appear in that column for the new row, but somehow when I click on the cell the date goes away and i get an error cos it is now blank. please help

  13. #13

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview row issue

    please help me. i am almost there

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