Usercontrol代码:
private string lastName;
public string LastName
{
get { return lastName; }
set
{
lastName = value;
textBox1.Text = value;
}
}
表格代码:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee", myDatabaseConnection))
{
int i = 0;
SqlDataReader DR1 = SqlCommand.ExecuteReader();
while (DR1.Read())
{
i++;
UserControl2 usercontrol = new UserControl2();
usercontrol.Tag = i;
usercontrol.LastName = (string)DR1["LastName"];
flowLayoutPanel1.Controls.Add(usercontrol);
}
}
}
使用上面的代码,我可以在每个usercontrol的文本框中显示数据库中的每个LastName。如何将数据库中的图片显示给每个用户控件的图片框?
这是我从数据库中显示单个图像的方式:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where ID = @a", myDatabaseConnection))
{
SqlCommand.Parameters.AddWithValue("@a", textBox1.Text);
DataSet DS = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
da.Fill(DS, "Images");
var imagesTable = DS.Tables["Images"];
var imagesRows = imagesTable.Rows;
var count = imagesRows.Count;
if (count <= 0)
return;
var imageColumnValue =
imagesRows[count - 1]["Image"];
if (imageColumnValue == DBNull.Value)
return;
var data = (Byte[])imageColumnValue;
using (var stream = new MemoryStream(data))
{
pictureBox1.Image = Image.FromStream(stream);
}
}
}
答案 0 :(得分:1)
要一次访问多个控件(在您的情况下为PictureBoxes),请使用foreach
循环。
foreach (Control control in this.Controls)
{
if (control is PictureBox)
{
PictureBox pic = (PictureBox)control;
pic.Image = Image.FromStream(stream); //something similar, this will only load the same image to every PictureBox
}
}
答案 1 :(得分:1)
为UserControl构造函数重载以接收姓氏和图像字节。然后你可以在循环中执行此操作:
while (DR1.Read())
{
i++;
UserControl2 usercontrol = new UserControl2((string)DR1["LastName"], (Byte[])DR1["Image"]);
usercontrol.Tag = i;
flowLayoutPanel1.Controls.Add(usercontrol);
}
这是更新的UserControl(使用其他问题的InvokeOnClick()代码)。请注意新构造函数中我们如何从字节创建图像并将其分配给PictureBox:
public partial class UserControl2 : UserControl
{
private string lastName;
public string LastName
{
get { return lastName; }
set
{
lastName = value;
textBox1.Text = value;
}
}
public UserControl2(string LastName, byte[] data)
{
InitializeComponent();
WireAllControls(this);
this.LastName = LastName;
try
{
using (var stream = new System.IO.MemoryStream(data))
{
pictureBox1.Image = Image.FromStream(stream);
}
}
catch (Exception)
{
MessageBox.Show("Error Loading Image for " + LastName);
}
}
public UserControl2()
{
InitializeComponent();
WireAllControls(this);
}
private void WireAllControls(Control cont)
{
foreach (Control ctl in cont.Controls)
{
ctl.Click += ctl_Click;
if (ctl.HasChildren)
{
WireAllControls(ctl);
}
}
}
private void ctl_Click(object sender, EventArgs e)
{
this.InvokeOnClick(this, EventArgs.Empty);
}
}
答案 2 :(得分:0)
Usercontrol代码:
public void showpictures()
{
{
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where LastName = @a", myDatabaseConnection))
{
SqlCommand.Parameters.AddWithValue("@a", textBox1.Text);
DataSet DS = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
da.Fill(DS, "Images");
var imagesTable = DS.Tables["Images"];
var imagesRows = imagesTable.Rows;
var count = imagesRows.Count;
if (count <= 0)
return;
var imageColumnValue =
imagesRows[count - 1]["Image"];
if (imageColumnValue == DBNull.Value)
return;
var data = (Byte[])imageColumnValue;
using (var stream = new MemoryStream(data))
{
pictureBox1.Image = Image.FromStream(stream);
}
}
}
}
}
表格
while (DR1.Read())
{
i++;
UserControl2 usercontrol = new UserControl2();
usercontrol.Tag = i;
usercontrol.LastName = (string)DR1["LastName"];
usercontrol.showpictures();
flowLayoutPanel1.Controls.Add(usercontrol);
}