我想找到另一种正确的方法来解决如何从XtragridControl网格视图中的选定行检索图片或图像到表单中的图片框。
这就是情景。
我有一个带有多个按钮的功能区控件,第一个按钮打开客户数据,您可以在其中找到一个带有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 :(得分:0)
我会使用强类型数据集或EF来将表格的内容绑定到网格。 那么你就不需要考虑加载和保存数据了。
由于您使用的是devexpress,我还会使用DataLayoutControl来绑定数据源中的不同字段。
您已将图像声明为字节,这是一个错字吗? 使用byte []或Image
如果你想使用你的类,用属性替换你的变量,然后你可以将它们绑定到gui控件,它也是一个更好的设计来使用类中的属性。