Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want find another proper approach on how to retrieve a picture or image from a selected row in XtragridControl gridview ;to picture box in the form ,this the scenario : i have a ribbon control with multiple buttons ,the first button opens the Customer data , where you find a user control with 2 buttons first button which open the customer form to add new customer and the second button opens the edit customer form where u edit customer after selecting a particular customer from the grid, i want a simpler approach that can allow me to retrieve the image or photo of the customer along with all other information . find below a sample of my code :
C#
// this my class Holding Field from table in database

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  using System.Drawing;

  namespace YoungWoman
  {
      public static class GetRowData
     {

  public static int LesseeId;

  public static byte image; //



  public static string LesseeName;
  public static string LesseeLastName;
  public static string PassportNo;
  public static string IDNo;
  public static DateTime BirthDate;
  public static string Gender;
  public static string Country;
  public static string City;
  public static string Province;
  public static string LesseePostalCode;
  public static string MobileNo;
  public static string HomePhoneNo;
  public static string TutorName;
  public static string TutorLastName;
  public static string AddressTutor;
  public static string AddressLessee;
  public static string TutorPhoneNo;
  public static string TutorEmail;




  }
 }


      this user control where u find the XtragridControl this how i retrieve values from the grid to get the form populated .


     private void EditLesseeFrm_Click(object sender, EventArgs e)
   {
       Lesseefrm Lessee = new Lesseefrm(Utils.Formtype.edit, 1);
       Lessee.LesseeEventHandler += new EventHandler(RefreshLesseeGrid);
       Lessee.RefreshLabeslEvent += new EventHandler(RefreshingLabels);


       GetRowData.LesseeId = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LesseeId"));
       //GetRowData.LesseePic = ByteImageConve(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LesseePic")); // this line of code return an error
       GetRowData.LesseeName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LesseeName"));
       GetRowData.LesseeLastName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"LesseeLastName"));
       GetRowData.PassportNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"PassportNo"));
       GetRowData.Gender = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Gender"));
       GetRowData.Province = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Province"));
       GetRowData.BirthDate = Convert.ToDateTime(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "BirthDate"));
       GetRowData.City = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "City"));
       GetRowData.Country = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Country"));
       GetRowData.MobileNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MobileNo"));
       GetRowData.HomePhoneNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "HomePhoneNo"));
       GetRowData.IDNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "IDNo"));
       GetRowData.AddressLessee = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "AddressLessee"));
       GetRowData.AddressTutor = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "AddressTutor"));
       GetRowData.LesseePostalCode = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "LesseePostalCode"));
       GetRowData.TutorName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorName"));
       GetRowData.TutorLastName = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorLastName"));
       GetRowData.TutorPhoneNo = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorPhoneNo"));
       GetRowData.TutorEmail = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TutorEmail"));

       Lessee.ShowDialog();
   }


      // this the form to receive the values from the grid to update



           if (formtype == Formtype.edit && Lesseeid > 0)
           {



               LesseeIdtextEdit.Enabled = false;
               ClearBtnlayoutControlItem26.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
               SaveBtn.Text = "&Edit";
               SaveBtn.Image = Resources.brush_16;
               this.Text = string.Format(" Edit Lessee Information - YW Residence ");

               LesseeIdtextEdit.Text = Convert.ToInt32(GetRowData.LesseeId).ToString();
               txtName.Text = GetRowData.LesseeName;
               txtLAstname.Text = GetRowData.LesseeLastName;
               txtPassport.Text = GetRowData.PassportNo;
               txtID.Text = GetRowData.IDNo;
               GendercomboBoxEdit.SelectedItem = GetRowData.Gender;
               DobdateEdit.DateTime = GetRowData.BirthDate;
               CountrycomboBoxEdit.SelectedItem = GetRowData.Country;
               txtProvince.Text = GetRowData.Province;
               txtCity.Text = GetRowData.City;
               txtPostalCode.Text = GetRowData.LesseePostalCode;
               LesseememoEdit1.Text = GetRowData.AddressLessee;
               txtMobile.Text = GetRowData.MobileNo;
               txtHomePhone.Text = GetRowData.HomePhoneNo;
               txtTutorName.Text = GetRowData.TutorName;
               txttutorLastname.Text = GetRowData.TutorLastName;
               tutorAddresstxt.Text = GetRowData.AddressTutor;
               txtTutorMobile.Text = GetRowData.TutorPhoneNo;
               txtEmail.Text = GetRowData.TutorEmail;



           }


      and this how i save before proceeding to any update



            private void SaveBtn_Click(object sender, EventArgs e)
   {

       if (formtype == Formtype.add)
       {
           if (txtPassport.Text.Length >= 8)
           {
               dxErrorProvider1.ClearErrors();



               try
               {

                   MemoryStream ms = new MemoryStream();
                   PicBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                   byte[] Pic_arr = new byte[ms.Length];
                   ms.Position = 0;
                   ms.Read(Pic_arr, 0, Pic_arr.Length);

                   da.InsertCommand = new SqlCommand(" insert into LesseeYW (LesseePic,LesseeName,LesseeLastName,PassportNo,IDNo,BirthDate,Gender,Country,LesseePostalCode,City,Province,MobileNo,HomePhoneNo,TutorName,TutorLastName,AddressTutor,AddressLessee,TutorPhoneNo,TutorEmail)Values(@image1,@name,@LastName,@pass1,@IDno,@DOB,@Gender,@Country1,@Postal,@City,@Province,@Mobile,@Phone,@Tname,@TLast,@address1,@address2,@Tphone,@Email)", conne);
                   da.InsertCommand.Parameters.AddWithValue("@id", GetRowData.LesseeId);
                   da.InsertCommand.Parameters.AddWithValue("@image1", Pic_arr);
                   da.InsertCommand.Parameters.AddWithValue("@name", txtName.Text);
                   da.InsertCommand.Parameters.AddWithValue("@LastName", txtLAstname.Text);
                   da.InsertCommand.Parameters.AddWithValue("@pass1", txtPassport.Text);
                   da.InsertCommand.Parameters.AddWithValue("@IDno", txtID.Text);
                   da.InsertCommand.Parameters.AddWithValue("@DOB", DobdateEdit.DateTime);
                   da.InsertCommand.Parameters.AddWithValue("@Gender", GendercomboBoxEdit.SelectedItem.ToString());
                   da.InsertCommand.Parameters.AddWithValue("@Country1", CountrycomboBoxEdit.SelectedItem.ToString());
                   da.InsertCommand.Parameters.AddWithValue("@Postal", txtPostalCode.Text);
                   da.InsertCommand.Parameters.AddWithValue("@City", txtCity.Text);
                   da.InsertCommand.Parameters.AddWithValue("@Province", txtProvince.Text);
                   da.InsertCommand.Parameters.AddWithValue("@Mobile", txtMobile.Text);
                   da.InsertCommand.Parameters.AddWithValue("@Phone", txtHomePhone.Text);
                   da.InsertCommand.Parameters.AddWithValue("@Tname", txtTutorName.Text);
                   da.InsertCommand.Parameters.AddWithValue("@TLast", txttutorLastname.Text);
                   da.InsertCommand.Parameters.AddWithValue("@address1", tutorAddresstxt.Text);
                   da.InsertCommand.Parameters.AddWithValue("@address2", LesseememoEdit1.Text);
                   da.InsertCommand.Parameters.AddWithValue("@Tphone", txtTutorMobile.Text);
                   da.InsertCommand.Parameters.AddWithValue("@Email", txtEmail.Text);


                   da.InsertCommand.ExecuteNonQuery();
                   conne.Close();
                   MessageBox.Show(" Lessee Data Successfully Added ", " Confirmation Lessee Data ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   this.Close();
               }

               catch (Exception Ex)
               {
                   MessageBox.Show(Ex.Message);
               }
           }

           else
           {
               MessageBox.Show("Invalid PassportNo","Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               dxErrorProvider1.SetError(txtPassport, "Invalid Passport No");

           }


           if (LesseeEventHandler != null)
           {
               LesseeEventHandler(sender, e);
           }

           if (LsEventHandler != null)
           {
               LsEventHandler(sender, e);
           }

           if (SelectEventHandler != null)
           {
               SelectEventHandler(sender, e);
           }
           if (LabelsEvent != null)
           {
               LabelsEvent(sender, e);
           }


       }
Posted
Updated 9-Sep-12 22:35pm
v3

1 solution

C#
Using System.IO;
Private void button1_click(object sender, EventArgs e)
{
int i;
try
{ 
i = dataGridView1.selectedCells[o].RowIndex;
byte[] img =(byte[])dataGridview1.Rows[i].Cells[2].value;
 MemoryStream ms = new MemoryStream(img);
ms.Read(img, 0, System.Convert.ToInt32(img.length));
Image im = Image.FromStream(ms);
//instantiating the form the image is copied to say form2
Form2 frm = new Form2();
frm.firstname.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
frm.pictureBox1.Image = im;
this is a simple example on how to copy the firstname along with image from a datagrid selected row by the click of a button to another form.

note to run the above code the picturebox and the textbox are made public.

Dnt forget to mark as answer if this helps you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900