Results 1 to 5 of 5

Thread: [RESOLVED] datagridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Resolved [RESOLVED] datagridview

    I have a form containing the following datagridview DGV as shown below.

    The DGV content displayed is from a database table (ACCESS). I would like to be able to input content directly into the last two columns. I had thought that this would be a simple process of setting some properties, but that appears not to be the case. Additionally, I have not even been able to find content that explains the process.

    Can someone suggest what I am missing that I am unable to find an explanation of the process, assuming it exists? Or better still, explain the process?

    Name:  2022-04-22_16-37-13.jpg
Views: 487
Size:  25.8 KB

    The DGV is contained as part of a form. However, the DGV is completely independent of all else on the form.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Re: datagridview

    OK, I have been looking and have a partial resolution, although not what I was wanting.

    So I used the following code to load the data I wanted into the DGV. The DGV. The code is executed from the save button on the form

    Code:
        Private Sub UpdatePart()
            'Assembly parameters
            For i = 0 To dgvComponent.Rows.Count - 1
                MasterBase.AddParam("@amount", dgvComponent.Rows(i).Cells(3).Value)
                MasterBase.AddParam("@unit", dgvComponent.Rows(i).Cells(4).Value)
                MasterBase.MasterBaseQuery("UPDATE adjAssembly " &
                                       "SET colAmount=@amount,colUnit=@unit")
            Next
        End Sub
    I then input the data into columns 3 and 4 in the DGV

    Attachment 184700

    Everything appeared to work well... except column 3 was updated to 75 for every record, instead of the two records being updated as displayed in the DGV.

    I stepped through the code and it only went through 2 times. I believe the code is correct, but then I have been wrong so many times before.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: datagridview

    This code will update every record with the same value,

    Code:
    "UPDATE adjAssembly " &
                                       "SET colAmount=@amount,colUnit=@unit"
    You need to identify the record you want updated, usually there is a PrimaryKey

    Code:
    "UPDATE adjAssembly " &
                                       "SET colAmount=@amount,colUnit=@unit WHERE yourIdField= @IdParam"
    But if you are using a DataAdapter to retrieve the data then it would be easier to us the DataAdapter Update method.

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

    Re: datagridview

    Any example that demonstrates how to retrieve and save data using a data adapter is relevant. Here's one I prepared earlier. You simply create a data adapter with the appropriate SelectCommand and UpdateCommand, or use a command builder to generate the UpdateCommand for 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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Re: datagridview

    So basically, my cool method using the For/Next loop is bat dung! OK, I can accept that.

    I have reviewed your suggestions and I think I can pull what I need to get what I need. Thanks.

Tags for this Thread

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