1

I'm trying to set up a ListView that will output all the results from a search query with LiteDB. Unfortunately, I'm having trouble finding a way to get the results into the ProductName string format. I've tried multiple different ways to no success. The code below clearly isn't right. However, is just a quick example of what I'm trying to do.

private void SearchButton_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    using (var db = new LiteDatabase(@"C:\Temp\MyData.db"))
    {
        var collection = db.GetCollection<Product>("Product");
        var results = collection.Find(x => x.ProductName.StartsWith(textBox1.Text));
        foreach(var item in results)
        {
            listView1.Items.Add(item.ToString());
        }
    }
}

I expected the results to be the ProductName in a string format, however, it only outputs "StockMngr.Product" to the listview, StockMngr being the name of the project.

Steve
  • 213,761
  • 22
  • 232
  • 286
kal256
  • 11
  • 2
  • You'll have to either print the properties of `item` yourself here, e.g. `item.ProductName` rather than trying to print the whole object at once with `item.ToString()`, or override `ToString` in the Product class with however you'd like to format the output. (This is your Product class and how you're using it, not LiteDB.) – Rup Sep 11 '19 at 09:51
  • @Rup Amazing, I tried that earlier and it didn't work for some reason. But your explanation made more sense. Works just how I want it now. Thank you. – kal256 Sep 11 '19 at 10:32

0 Answers0