我遇到了一个小问题。我想在单击ListView控件行时从我的Sql表中获取要在Image控件中显示的图像数据。我正在使用C#WPF和Microsoft SQL。
现在我已经可以将图像插入到我的数据库中,但我遇到的问题是,一旦点击ListView中的某行,我似乎无法检索图像数据? 这是我尝试过的编码......
这是我用来填充ListView的编码:
private void FillEmployeeListView()
{
string ConString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string CmdString = string.Empty;
using (SqlConnection CON = new SqlConnection(ConString))
{
CmdString = "SELECT EmployeeEmailAddress, EmployeePassword, EmployeeDepartment, EmployeeIDNumber, EmployeeName, EmployeeSurname, EmployeeGender, EmployeeDOB, EmployeeHomeAddress, EmployeeTelephoneNumber, EmplyeeCity, EmployeeProvinceCode, EmployeeProfilePicture FROM Main.tblEmployeeLoginDetails";
SqlCommand CMD = new SqlCommand(CmdString, CON);
SqlDataAdapter SDA = new SqlDataAdapter(CMD);
DataTable DT = new DataTable("Main.tblEmployeeLoginIDetails");
SDA.Fill(DT);
lvDisplayEmployeeInformation.ItemsSource = DT.DefaultView;
}
}
我的ListView的SelectionChanged事件:
private void lvDisplayEmployeeInformation_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedRow = lvDisplayEmployeeInformation.SelectedIndex;
if (selectedRow != -1)
{
DataView DS = (DataView)lvDisplayEmployeeInformation.ItemsSource;
string ColumnEmailAddress = DS.Table.Rows[selectedRow].ItemArray[0].ToString();
string ColumnPassword = DS.Table.Rows[selectedRow].ItemArray[1].ToString();
string ColumnDepartment = DS.Table.Rows[selectedRow].ItemArray[2].ToString();
string ColumnIDNumber = DS.Table.Rows[selectedRow].ItemArray[3].ToString();
string ColumnName = DS.Table.Rows[selectedRow].ItemArray[4].ToString();
string ColumnSurname = DS.Table.Rows[selectedRow].ItemArray[5].ToString();
string ColumnGender = DS.Table.Rows[selectedRow].ItemArray[6].ToString();
string ColumnDOB = DS.Table.Rows[selectedRow].ItemArray[7].ToString();
string ColumnHomeAddress = DS.Table.Rows[selectedRow].ItemArray[8].ToString();
string ColumnTelephoneNumber = DS.Table.Rows[selectedRow].ItemArray[9].ToString();
string ColumnCity = DS.Table.Rows[selectedRow].ItemArray[10].ToString();
string ColumnProvinceCode = DS.Table.Rows[selectedRow].ItemArray[11].ToString();
string ColumnProfilePicture = DS.Table.Rows[selectedRow].ItemArray[12].ToString();
txtEmailAddress.Text = ColumnEmailAddress;
pbPassword.Password = ColumnPassword;
txtDepartment.Text = ColumnDepartment;
txtIDNumber.Text = ColumnIDNumber;
txtName.Text = ColumnName;
txtSurname.Text = ColumnSurname;
txtGender.Text = ColumnGender;
txtDOB.Text = ColumnDOB;
txtHomeAddress.Text = ColumnHomeAddress;
txtTelephoneNumber.Text = ColumnTelephoneNumber;
txtCity.Text = ColumnCity;
txtProvinceCode.Text = ColumnProvinceCode;
imgProfilePicture.Source = _img;
最后,这是我想用来转换/插入和从我的数据库中检索我的图像的编码:
private void SetImage(Binary data)
{
if (data == null)
{
imgProfilePicture.BeginInit();
imgProfilePicture.Source = null;
imgProfilePicture.EndInit();
return;
}
_img = new BitmapImage();
_img.BeginInit();
_img.StreamSource = new MemoryStream(data.ToArray());
_img.EndInit();
imgProfilePicture.BeginInit();
imgProfilePicture.Source = _img;
imgProfilePicture.EndInit();
}
private Binary GetImageBinary()
{
if (imgProfilePicture.Source == null)
return null;
Stream stream = _img.StreamSource;
Byte[] buffer = null;
if (stream != null && stream.CanRead && stream.Length > 0)
{
//Die Binary reader return 'n empty byte array :(
using (BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((int)stream.Length);
}
}
return new Binary(buffer);
}
一旦我点击listview中的一行,我就可以将数据库中的文本插入到我的文本框中了。任何想法?
编辑:我得到的错误如下 - “找不到适合完成此操作的成像组件。”