Search code examples
c#databaselitedb

Showing LiteDB database items in data grid view


I'm using LiteDB. Customer definition is like below:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string[] Phones { get; set; }
    public string[] Cars { get; set; }
}

When I want to show my data in data grid view only first two columns are shown and data grid view doesn't show last two columns which are array.

and this is GetAll

private List<Customer> GetAll()
    {
        var issuesToReturn = new List<Customer>();
        try
        {
            using (var db = new LiteDatabase(Constants.ConnectionString))
            {
                var issues = db.GetCollection<Customer>("customers");
                foreach (Customer issueItem in issues.FindAll())
                {
                    issuesToReturn.Add(issueItem);
                }
            }
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message);
        }
        return issuesToReturn;
    }

Solution

  • You can try use another field just to format data in you grid (but not stored in datafile). Like this:

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string[] Phones { get; set; }
        public string[] Cars { get; set; }
    
        [BsonIgnore]
        public string DisplayPhones => string.Join(", ", Phones);
    
        [BsonIgnore]
        public string DisplayCars => string.Join(", ", Cars);
    }
    

    Then, change in you grid to map your column using this "Display" properties.