2012-09-09 28 views
1

我想找到另一種正確的方法來檢索圖片或圖像從XtragridControl gridview中的選定行到窗體中的圖片框。如何從選定的GridView行中檢索圖像

這是場景。

我有多個按鈕功能區控制,第一個按鈕打開客戶數據,在那裏你會發現有2個按鈕第一按鈕,打開客戶表單添加新客戶,第二個按鈕的用戶控件打開編輯客戶表格在從電網選擇特定客戶後,您可以在其中編輯客戶,我想要一個更簡單的方法,可以讓我檢索客戶的圖像或照片以及所有其他信息。

找到我的代碼示例如下:

// 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 containing the XtragridControl and 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); 
      } 


     } 

回答

0

我會用一個強類型數據集或EF,表中的內容結合到您的網格。 然後,您不需要考慮如何加載和保存數據。

由於您使用的是devexpress,我還會使用DataLayoutControl來綁定數據源中的不同字段。

你已經宣佈你的圖像爲字節,是一個錯字? 使用字節[]或圖像

如果您想使用您的類,您的變量與屬性替換,那麼你可以綁定他們到GUI控件,它也是一個更好的設計使用類中的屬性。

相關問題