Results 1 to 7 of 7

Thread: Selected row in datagridview to other form (including picture in one cell)?

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Location
    Copenhagen, Denmark
    Posts
    185

    Question Selected row in datagridview to other form (including picture in one cell)?

    Hi everyone

    I have this datagridview which is populated from a MySQL database (online). When user selects a rows and clicks a button I have another form opening where the data from the selected rows should be presented in textboxes and one picturebox. The user shall now be able to edit the data and save it again. I tried several weeks now but I can't fin a solution that works.

    Hope you can help me? It is especially the picture from the cell that I can't get over to the picturebox in the other form.

    Let me know what info you need?

    Thanks in advance.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Selected row in datagridview to other form (including picture in one cell)?

    You can pass data between forms in multiple ways, jmc explains one of my favorite ways here http://jmcilhinney.blogspot.com.au/2...orms-part.html

    If you can't find a way to pass the picture then I wouldn't use two forms, I would add a panel, with the necessary controls to display the data, to the form. Keep the panel hidden until you want to use it.

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

    Re: Selected row in datagridview to other form (including picture in one cell)?

    You might follow the CodeBank link in my signature below and check out my thread on Editing A Grid Row In A Dialogue. In short, you can pass the DataRow bound to your grid row to the dialogue and it pulls the data out of each field and displays them in its controls. If the user clicks the Cancel button then the form simply closes and you're done. If the user clicks OK then the dialogue pushes the data from its controls back into the DataRow and then closes. Mpte that the dialogue has exactly ZERO interaction with the database. It is the responsibility of the calling form, which retrieved the data in the first place, to save any changes back to the database. It knows to do that when the ShowDialog call on the dialogue returns OK.

    As for the image, it is stored in the DataRow as a Byte array. To display it in a PictureBox, you'll need to create an Image object from that Byte array. If you want to be able to replace it, you'll need to create a Byte array from the new Image. To learn how to do that, check out my CodeBank thread on Saving Images In Databases.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Location
    Copenhagen, Denmark
    Posts
    185

    Re: Selected row in datagridview to other form (including picture in one cell)?

    Perfect, I'll look into it and then if I have problems getting it to work I'll get back to you.

    Thanks so far.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Location
    Copenhagen, Denmark
    Posts
    185

    Re: Selected row in datagridview to other form (including picture in one cell)?

    Hi again,

    I got most of it to work but when trying to save an update from the second form I get this error. Can't figure it out. :-(

    Name:  error.jpg
Views: 116
Size:  27.4 KB

    This is the code for the save updatebutton:

    Code:
     Private Sub ButtonSaveUpdate_Click(sender As Object, e As EventArgs) Handles ButtonSaveUpdate.Click
            MysqlConnUp = New MySqlConnection
            MysqlConnUp.ConnectionString = "server=My.server;user=my-username;password=*************;database=MyDatabase"
            Dim mstream As New System.IO.MemoryStream
            PictureBoxBillede.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim arrImage() As Byte = mstream.GetBuffer()
            mstream.Close()
    
            If CheckBoxFornyelse.CheckState = CheckState.Checked Then
                forny = "Ja"
            Else
                forny = "Nej"
            End If
            If CheckBoxNyProeve.CheckState = CheckState.Checked Then
                nyProeve = "Ja"
            Else
                nyProeve = "Nej"
            End If
    
            Dim Sql As String
            Sql = "UPDATE HVS.CERTIFIKAT SET  fornavn='" & TextBoxFornavn.Text & "',efternavn='" & TextBoxEfternavn.Text & "',adresse='" & TextBoxAdresse.Text & "',postnummer='" & MaskedTextBoxPostnr.Text & "',`by`='" & TextBoxBy.Text & "',tlf='" & TextBoxTlf.Text & "',mobil='" & TextBoxMob.Text & "',email='" & TextBoxEmail.Text & "',CPR='" & MaskedTextBoxCpr.Text & "',certifikat='" & ComboBoxCertifikat.Text & "',proevedato='" & DateTimePickerUdstedelse.Text & "',udloebsdato='" & TextBoxUdloeb.Text & "',kraever_fornyelse='" & forny & "',kraever_ny_proeve='" & nyProeve & "', Billede=@Billede" & "WHERE Id LIKE '" & MaskedTextBoxId.Text & "'"
    
            Try
                With COMMANDUPDATE
                    .Connection = MysqlConnUp
                    .CommandText = Sql
                    .Parameters.AddWithValue("@Billede", arrImage)
                End With
                COMMANDUPDATE.ExecuteNonQuery()
            Catch ex As MySqlException
                MessageBox.Show(ex.Message)
                MsgBox(ex.ToString)
            Finally
                MysqlConnUp.Dispose()
            End Try
            Listview.Show()
            Me.Close()
        End Sub
    Last edited by lovschal; Jan 1st, 2018 at 01:42 PM.

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

    Re: Selected row in datagridview to other form (including picture in one cell)?

    If you can't figure that one out then you can't really have done much research on the subject. A NullReferenceException means that you are trying to access a member of an object that doesn't exist. As there's only one reference on the line that generates the exception, the source is obvious.

    Also, building a SQL statement that way, i.e. with string concatenation, is a very, VERY bad idea. You are doing the right thing and using a parameter in one case but, it appears, only because you have to. You should be using a parameter for every one of those values. You clearly know how to use parameters so I'm not going to explain how it's done.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Location
    Copenhagen, Denmark
    Posts
    185

    Re: Selected row in datagridview to other form (including picture in one cell)?

    Yes, just changed it to parameters instead. It’s also easier to find my way around the gode that way.

    Found the fault aften a while, I forgot New in the generation of the COMMANDUPDATE. Added that and it worked. Shouldn’t be coding in the middel of the night. ;-)

    Thanks anyway for the help.

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