2012-09-09 30 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控件,它也是一个更好的设计使用类中的属性。

相关问题