gpt4 book ai didi

c# - 为每一行动态创建控件 C#

转载 作者:行者123 更新时间:2023-11-30 23:00:59 24 4
gpt4 key购买 nike

问题

我正在尝试为包含图像的每一行创建一个控件。

private void ass_wijzig_Load(object sender, EventArgs e)
{
//query's
string count = "select COUNT(Image) from product";
string query = "select image from product";
//commands
MySqlCommand cmd = new MySqlCommand(count, connection);
cmd.CommandType = CommandType.Text;
int totalcount = Convert.ToInt32(cmd.ExecuteScalar());
MySqlCommand cmd2 = new MySqlCommand(query, connection);
MySqlDataReader reader = cmd2.ExecuteReader();
if (reader.Read())
{
for (int i = 0; i <= totalcount; i++)
{
//What do I need to put here?
}
}

案例

程序查看我的产品表中有多少行。每个创建的行都包含一个图像。我希望程序检查有多少行(有效),然后创建一个包含该行图像的图片框。如您所见,我已经确保每一行都被计算在内。有人告诉我不要使用数组。要从数据库中检索图片,您需要使用数据适配器和数据表。

问题

如何为表格中的每一行创建一个包含正确图像的图片框?

附件

将一张图片放入图片框的代码

private void ass_wijzig_Load(object sender, EventArgs e)
{
string query = "Select Image From Product where productid = 1";
MySqlCommand cmd = new MySqlCommand(query, connection);
// MySqlDataReader reader = cmd.ExecuteReader();
var da = new MySqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Image");
int count = ds.Tables["Image"].Rows.Count;



if (count > 0)
{
var data = (Byte[])(ds.Tables["Image"].Rows[count - 1]["Image"]);
var stream = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(stream);
}

非常重要!,上面的代码仅用于确保我能够将图像放入图片框中,我包含此代码以支持我必须使用数据表和数据适配器。此代码不再包含在我的项目中。

提前致谢

最佳答案

这样试试

private void ass_wijzig_Load(object sender, EventArgs e)
{
string query = "Select Image From Product where productid = 1";
MySqlCommand cmd = new MySqlCommand(query, connection);
// MySqlDataReader reader = cmd.ExecuteReader();
var da = new MySqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Image");
int count = ds.Tables["Image"].Rows.Count;
List<PictureBox> pbList = new List<PictureBox>();

for (int i = 0; i < count-1; i++)
{
var data = (Byte[])(ds.Tables["Image"].Rows[i]["Image"]);
var stream = new MemoryStream(data);
pbList[i] = new PictureBox();
pbList[i].Name = "pic" + i;
pbList[i].Size = new Size(300, 75);
pbList[i].Image = Image.FromStream(stream);
myform.Controls.Add(pbList[i]);
}
}

更新

您不需要为 PictureBox 循环数组

private void ass_wijzig_Load(object sender, EventArgs e)
{
string query = "Select Image From Product where productid = 1";
MySqlCommand cmd = new MySqlCommand(query, connection);
// MySqlDataReader reader = cmd.ExecuteReader();
var da = new MySqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Image");
int count = ds.Tables["Image"].Rows.Count;

for (int i = 0; i < count-1; i++)
{
var data = (Byte[])(ds.Tables["Image"].Rows[i]["Image"]);
var stream = new MemoryStream(data);
PictureBox pbList = new PictureBox();
pbList.Name = "pic" + i;
pbList.Size = new Size(300, 75);
pbList.Image = Image.FromStream(stream);
myform.Controls.Add(pbList);
}
}

关于c# - 为每一行动态创建控件 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23991154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com