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