Upload Datagridview photo and move to picturebox C#

Asked

Viewed 112 times

1

Guys with little difficulty in picking up my registered photo and show it in Picturebox

My scenario is this::

I have a registration form that makes the insertion of data and A form that searches the register that is presented in a Datagridview; Well I do the insertion of the data and the photo converted to binary, using the db sqlserverm ais to with difficulty in passing the photo to my picture, the other fields I can bring, but not the photo.

Note: it’s my first time working with photo on Windowsforms.

follow my code, guys.

<-- This is from my Datagridview where I select the cell playing the data to the registration form-->

private void dgw_Onibus_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        try
        {
            if (lblOperacao.Text == "Aluno Master")
            {

                DataGridViewRow dr = dgw_Onibus.SelectedRows[0];
                this.Hide();
                cg.ds = new DataSet();
                FRM_CadOnibusCircular frm = new FRM_CadOnibusCircular();
                frm.Show();

                frm.TB_id.Text = dr.Cells[0].Value.ToString();
                frm.TB_IdFicha.Text = dr.Cells[1].Value.ToString();
                frm.TB_NomeUser.Text = dr.Cells[2].Value.ToString();
                frm.MK_TelUser.Text = dr.Cells[3].Value.ToString();
                frm.TB_RgUser.Text = dr.Cells[4].Value.ToString();
                frm.MTK_CpfUser.Text = dr.Cells[5].Value.ToString();

                frm.TB_NomeProprietario.Text = dr.Cells[7].Value.ToString();
                frm.MK_TelProprietario.Text = dr.Cells[8].Value.ToString();
                frm.TB_EnderecoProprietario.Text = dr.Cells[9].Value.ToString();
                frm.TB_QdProprietario.Text = dr.Cells[10].Value.ToString();
                frm.TB_EmailProprietario.Text = dr.Cells[11].Value.ToString();
                frm.TB_RgProprietario.Text = dr.Cells[12].Value.ToString();
                frm.MKT_Proprietario.Text = dr.Cells[13].Value.ToString();
                frm.TB_NumeroProprietario.Text = dr.Cells[14].Value.ToString();
                frm.TB_LoteProprietario.Text = dr.Cells[15].Value.ToString();

                bool proprietarioChecked = dgw_Onibus.CurrentRow.Cells["opcao"].Value.ToString().Contains("Proprietario");
                bool depedenteChecked = dgw_Onibus.CurrentRow.Cells["Opcao"].Value.ToString().Contains("Depedente");
                bool colaboradorChecked = dgw_Onibus.CurrentRow.Cells["opcao"].Value.ToString().Contains("Colaborador"); // Ou contém "Yoga", não sei como você guarda isto na sua `DataGridView`

                // Obter checkBoxlist da seleção

                int indexCbProprietario = GetItemIndex(frm.CLB_Opcao, "Proprietario");
                int indexCbDepedente = GetItemIndex(frm.CLB_Opcao, "Depedente");
                int indexCbColaborador = GetItemIndex(frm.CLB_Opcao, "Colaborador"); // fmr.chekedListBox é o nome do seu CheckedListBox dentro do seu form. 

                //Pegar o valor da dataGridView que diz se a determinada atividade está marcada





                // Checar o valor da Opcao no seu `checkedListBox`
                frm.CLB_Opcao.SetItemChecked(indexCbProprietario, proprietarioChecked);
                frm.CLB_Opcao.SetItemChecked(indexCbDepedente, depedenteChecked);
                frm.CLB_Opcao.SetItemChecked(indexCbColaborador, colaboradorChecked);



                frm.BT_UpdateOnibus.Enabled = true;
                frm.BT_DeleteOnibus.Enabled = true;
                frm.BT_SaveOnibus.Enabled = false;
                frm.Lbl_Usuario.Text = lblUsuario.Text;
                lblOperacao.Text = "";
            }
        }
        catch (Exception erro)
        {
            MessageBox.Show(erro.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

I tried that way but it didn’t work!

frm.picFoto.Image = null;

                if (ds.Tables[0].Rows[linhaNumero][6] != System.DBNull.Value)
                {
                    foto_array = (byte[])ds.Tables[0].Rows[linhaNumero][6];
                    MemoryStream ms = new MemoryStream(foto_array);
                    frm.picFoto.Image = Image.FromStream(ms);
                }

 int linhaNumero = 0;
    byte[] foto_array;

Note:"My column of the table where it stores the photo, it is defined as image, initially it had left as Varbinary, but I switched to tests." from now on I appreciate the help!

  • Hey Lucas, what didn’t work out? some error? your code looks ok, convert to byte array, put in a stream to then use the Image.FromStream, that’s right

  • me returns the error of "cannot Fild table0" when I click on the cell of datagridview to play the photo in the registration form to perform the update etc...

No answers

Browser other questions tagged

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