简体   繁体   English

如何动态填充Imagelist(数据库中的数据)C#

[英]How to populate Imagelist dynamically (data from database) C#

I need to populate an ImageList dynamically. 我需要动态填充ImageList Means I stored image names in a database table. 表示我将image names存储在数据库表中。 But images are in categories folder inside project folder (1.jpg, 2.jpg...) using a while loop I tried to add image into ImageList and bind to the ListView . 但是图像使用while循环位于项目文件夹(1.jpg, 2.jpg...)中的类别文件夹中(1.jpg, 2.jpg...)我尝试将图像添加到ImageList并绑定到ListView

Here is my code and TABLE. 这是我的代码和表。 No errors but images are not loading. 没有错误,但图像未加载。 I am not sure image path is correct or not. 我不确定图像路径是否正确。 What is wrong with my code and how to do that? 我的代码有什么问题,怎么做?

public void show()
{
    ImageList imgs = new ImageList();
    imgs.ImageSize = new Size(50,50);

    MySqlConnection con = cn.connection();
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT * FROM categories";
    MySqlDataReader rd;
    con.Open();
    rd = cmd.ExecuteReader();
    while (rd.Read())
    {
        try
        {
            imgs.Images.Add(Image.FromFile("./categories/" + rd.GetString(2)));
        }
        catch (Exception e) {
            MessageBox.Show(e.Message);
        }
    }
    listView1.SmallImageList = imgs;
    while (rd.Read())
    {
        listView1.Items.Add("asas",rd.GetString(0));
    }
}

TABLE

IMAGE PATH 图像路径

C:\vb\nishantha\pos3\categories

PROJECT FOLDER 项目文件夹

enter image description here 在此处输入图片说明

IMAGE FOLDER 图像文件夹

enter image description here 在此处输入图片说明

Try this 尝试这个

imgs.Images.Add(Bitmap.FromFile("\\categories\\" + rd.GetString(2)));

might help you achieve it And the next while loop might be a problem since running the first loop already read all the records from the DB. 可能会帮助您实现这一目标,而下一个while循环可能会成为问题,因为运行第一个循环已经从数据库读取了所有记录。 So if you want you can include the same in the first loop itself. 因此,如果您愿意,可以在第一个循环本身中包含相同的内容。

listViewItem lvi = new listViewItem();
while (rd.Read())
{
    try
    {
        imgs.Images.Add(Bitmap.FromFile("\\categories\\" + rd.GetString(2)));
        lvi.SubItems.Add(rd.GetString(0));
    }
    catch (Exception e) {
        MessageBox.Show(e.Message);
    }
}
listView1.Items.Add(lvi);
listView1.SmallImageList = imgs;

Make sure categories folder with files is available in root directory. 确保在目录的根目录中有包含文件的category文件夹。

eg if you are building the application in debug mode, then categories folder and files should be in ~/debug/categories/..jpg. 例如,如果您以调试模式构建应用程序,则category文件夹和文件应位于〜/ debug / categories / .. jpg中。 for doing this take below steps: 为此,请执行以下步骤:

Expand categories folder, select all files --> Right click --> Property --> CopyToOutputDirectory to Copy always. 展开类别文件夹,选择所有文件->右键单击->属性-> CopyToOutputDirectory以始终复制。

Finally in last line for adding the items in the listview change the below code: 最后,在用于添加列表视图中项目的最后一行中,更改以下代码:

while (rd.Read())
{
   listView1.Items.Add("asas",rd.GetString(0));
}

To

for (int j = 0; j < imgs.Images.Count; j++)
{
    ListViewItem item = new ListViewItem();
    item.ImageIndex = j;
    item.Text = "asdf";

    this.listView1.Items.Add(item);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM