c# change source of picturebox

Asked

Viewed 279 times

-2

I have a table with 4 Image fields. I put a pictureBox and in the properties Data, in Databindings, Inkei it with one of the fields of the table, the field image1, when executing the program is bringing right the image of the field image1 of the table. So it can make changes, draw or delete and when clicking save, it will save the changes in the field of the table to which it is linked "image1".

How do I make the user, when clicking a button, change which field of the table this pictureBox is linked to? For example, if you click on button 2, pictureBox shows the image that is in field image2 of the table, and any changes made, when clicking save, will already be saved in "image2".

I’m using the visual studio’s local database, just dragging and dropping the fields from the "Datasources" panel. I don’t use any sql code.

  • but as the source of the picturebox ?

  • I selected the pictureBox. Ai la in the properties window of the visual studio clicked on Databindings, it opens a screen where I chose my table, ai shows all the fields of this table, and I chose "image1".

1 answer

1


From what I’ve researched, you’ve added a Control.DataBindings when doing through the graphical interface of Visual Studio. With this, I believe that you can simply clean up the collection and add another Source:

    private void button1_Click(object sender, EventArgs e)
    {
         pictureBox1.DataBindings.Clear();
         pictureBox1.DataBindings.Add("Image", SeuDataSet.Tables["suaTabelaDeImagens"], "coluna2");

    }

Documentation:

https://msdn.microsoft.com/pt-br/library/system.windows.forms.control.databindings(v=vs.110). aspx

https://msdn.microsoft.com/pt-br/library/system.windows.forms.binding(v=vs.110). aspx

https://msdn.microsoft.com/pt-br/library/system.windows.forms.controlbindingscollection(v=vs.110). aspx

  • what would be this "yourTable" put number 0 for being my first table gave Null error, put the table name in quotes, gave error "convert string to int". And thanks for your help,

  • you should have a Dataset in your application, inside it, which table of images... as is your first, 0 should work, but only if the Dataset is already loaded

  • You are giving an invalid System.Dbnull Conversion error in System.Drawing.Image. Could it be because the field I’m typing is still empty? The table is empty even, the user who presses the button and selects the image of his pc with Openfiledialog. This could be it??

  • I believe so.

  • 1

    Got it, That’s exactly it, my friend. Thanks for all the help you give.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.