Reputation: 41
I want to add small image to existing row i listView. ListView is connected to SQLite DB. When select some row in listView and click on button "Zakoncz" there is added to SQLite DataBase value "1" to column "CzyZaznaczone" and when here is value "1" listView should add image to selected row. My image is in resources, can I use that or only from file? Now my code adds a value to the database but does not add an image.
Here is the code:
SQLiteConnection con = new SQLiteConnection("data source=baza.db");
SQLiteDataAdapter ada = new SQLiteDataAdapter("select * from Przypominacz", con);
SQLiteCommand cmd = con.CreateCommand();
var index = this.listView1.SelectedIndices[0];
string zaznaczone = this.listView1.Items[index].SubItems[0].Text;
con.Open();
cmd.CommandText = "UPDATE Przypominacz SET CzyZakonczone=1 WHERE Nazwa='"+ zaznaczone + "'";
cmd.ExecuteNonQuery();
//dodanie ikonki do zakonczonego zadania
bool result = false;
string sql = "SELECT * FROM Przypominacz WHERE CzyZaznaczone='1'";
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
result = true;
}
reader.Close();
bool rezultat = result;
if(rezultat==true)
{
for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
{
ImageList imageList = new ImageList();
imageList.Images.Add(Image.FromFile("D:\\C#\\Przypominacz2 — kopia (4)\\przypominacz\\przypominacz\\Resources\\checked.png"));
listView1.SmallImageList = imageList;
var listViewItem = listView1.Items.Add("Item with image");
}
}
Now looks: Without img
But should looks like this: With img
Upvotes: 1
Views: 4758
Reputation: 7095
You are adding new ListViewItem
to existing list instead of changing selected one(s). You can do it like this:
if(rezultat)
{
ImageList imageList = new ImageList();
imageList.Images.Add(Image.FromFile("D:\\C#\\Przypominacz2 — kopia (4)\\przypominacz\\przypominacz\\Resources\\checked.png"));
listView1.SmallImageList = imageList;
for (int i = 0; i < listView1.Items.Count; i++ )
{
if (listView1.Items[i].Selected)
listView1.Items[i].ImageIndex = 0;
}
}
To add image to ImageList
from resources, take a look at this question
Upvotes: 2