。 黑匣子是一个用户控件。根据数据库中记录的数量显示多个用户控件
用户控件代码
string b = "";
public string ID
{
set { b = value; }
}
public void sample()
{
textBox1.Clear();
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", b);
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);
}
}
}
}
public void getname()
{
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee where ID = @a", myDatabaseConnection))
using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
{
SqlCommand.Parameters.AddWithValue("@a", b);
SqlDataReader DR1 = SqlCommand.ExecuteReader();
if (DR1.Read())
{
textBox1.Text = DR1.GetString(DR1.GetOrdinal("LastName")).ToString();
}
}
}
}
表单代码
public string ID
{
get { return textBox1.Text; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
userControl21.ID = ID;
userControl21.sample();
userControl21.getname();
}
使用代码以上我可以显示单个记录到该用户控件。我如何根据数据库中的记录数和他们的图片和名称显示一些用户控件?例如,我在数据库中有7条记录,表单将显示7个用户控件。当我点击或选择一个用户控件时,地址,联系人等更多信息将以我的形式显示。如果数据库中的记录太多而无法放入表单中,则会出现垂直或水平滚动条。给我一些代码:)
什么是最好的容器来显示usercontols可以有滚动条?一个面板,groupbox或任何东西?
这必须是样本输出。 。
[动态添加/删除它们](http://stackoverflow.com/questions/4719476/create-controls-dynamically) – Sayse