2
votes

I've been googling to find an answer but I couldn't find. Here is my problem, I have a

  • database table call sold_item_details
  • datagrid with seven columns
  • ObservableCollection

1 Need to bind ObservableCollection with database

I want to get set of strings from database and need to add to ObservableCollection as rows. I want to achieve, something similar to following code,

 ObservableCollection<string> o = new ObservableCollection<string>();
 o.add("string1","string2","string3","string4","string5"); // this has to be one row
 o.add("string6","string7","string8","string9","string10");

likewise there are set of data sets. So far I've seen that Objects are added and from that this is achieved but I don't want to create objects for each set of strings.

2 Need to bind ObservableCollection with the datagrid

so datasets inside the ObservableCollection is added as rows to the datagrid (there are five columns)

Can anyone suggest me a method to achieve this?

EDIT: These are the codes what I've achieved,

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" CanUserResizeRows="False" Grid.ColumnSpan="8"
    Margin="-20,0,30.5,0" CanUserResizeColumns="False" CanUserReorderColumns="False"
    CanUserSortColumns="False" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding supplier_id}" CanUserResize="False" FontSize="16"
            Header="Supplier" Width="0.18*" />
        <DataGridTextColumn Binding="{Binding item_id}" CanUserResize="False" FontSize="16"
            Header="ItemID" Width="0.13*" />
        <DataGridTextColumn Binding="{Binding item_name}" CanUserResize="False" FontSize="16"
            Header="Item name" Width="0.2*" />
        <DataGridTextColumn Binding="{Binding weight}" CanUserResize="False" FontSize="16" Header="kg"
            Width="0.1*" />
        <DataGridTextColumn Binding="{Binding price_per_kg}" CanUserResize="False" FontSize="16"
            Header="Price/kg" Width="0.1*" />
        <DataGridTextColumn Binding="{Binding no_of_bags}" CanUserResize="False" FontSize="16"
            Header="Bags" Width="0.07*" />
        <DataGridTextColumn Binding="{Binding rent_amt}" CanUserResize="False" FontSize="16"
            Header="Rent" Width="0.07*" />
        <DataGridTextColumn Binding="{Binding total_price}" CanUserResize="False" FontSize="16"
            Header="Value" Width="0.15*" />
    </DataGrid.Columns>
</DataGrid>

OrderItem class properties of this class should goes to the columns of the datagrid

public class OrderItem
{
    public OrderItem(string supplierId,
        string itemId,
        string itemName,
        decimal weight,
        decimal price,
        int noOfBags,
        decimal rentAmt)
    {
        SupplierId = supplierId;
        ItemId = itemId;
        Weight = weight;
        Price = price;
        NoOfBags = noOfBags;
        RentAmt = rentAmt;

        BagPrice = // bag price will get from a method
        TotalPrice = // something

    }

    public string SupplierId { get; set; } // Supplier's ID
    public string ItemId { get; set; } // Item ID
    public string ItemName { get; set; }// Item Name
    public decimal Weight { get; set; } // weight of the item
    public decimal Price { get; set; } // Total sum of the order
    public int NoOfBags { get; set; } // Number of bags in a single order 
    public decimal BagPrice { get; set; }
    public decimal RentAmt { get; set; } // To weigh the item
    public decimal TotalPrice { get; private set; }
}

when adding an item it is sent to the ObservableCollection

ObservableCollection<OrderItem> orderItems = new ObservableCollection<OrderItem>();
//and at the right event (key enter is pressed) the orderItem object is added to the orderItems 
orderItems.add(orderI);

In the xaml code for grid I have already bound to the database directly what i need is to bind this to ObservableCollection

1
Why don't you create a datagrid with 5 columns and create a dataclass for that. So that the source of the datagrid could be bound to an ObservableCollection<dataclass> in your view model. You can set each column inside the template of the data grid. In this case, you need not add each item to the list. Maybe add all the items fetched from database to the ObservableCollection<dataclass>ViVi
@vishakh369 I didn't try that because in my application there will be more than 300 rows for the datagrid in a single time so that would be a wastage of memory creating objects and I want "just to display" it.Lahiru Jayathilake
How can that be a wastage of memory? If your source list is bound to the target, then it will be updated each time and a new row will be added.ViVi
@vishakh369 so every time when I have to display rows in the datagrid I have to add many "dataclass" object to the ObservableCollection, so wouldn't that be a memory wastage when compared to passing only five strings to the ObservableCollection vs passing an dataclass object?Lahiru Jayathilake
Thanks @vishakh369 that was a great helpLahiru Jayathilake

1 Answers

1
votes

Why don't you create a datagrid with 5 columns and create a dataclass for that. So that the source of the datagrid could be bound to an ObservableCollection in your view model. You can set each column inside the template of the data grid. In this case, you need not add each item to the list. Maybe add all the items fetched from database to the ObservableCollection.

Maybe the memory consumption would be less, but still a better design is to have a dataclass and a list of items that binds to the datagrid. These 5 strings would be serving some purpose, right? For further computation or calculation, it would be better to reuse the fetched data by using an ObservableCollection for your requirement. Anyways it’s upto you. If you want you could hold on to the way of passing 5 strings also.