【发布时间】:2016-08-28 02:31:04
【问题描述】:
我一直在谷歌上寻找答案,但我找不到。 这是我的问题,我有一个
- 数据库表调用sold_item_details
- 七列数据网格
- ObservableCollection
1 需要将 ObservableCollection 与数据库绑定
我想从数据库中获取一组字符串,并且需要添加到 ObservableCollection 作为 行。我想实现,类似于下面的代码,
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");
同样有一组数据集。到目前为止,我已经看到添加了对象并从中 这是实现的,但我不想为每组字符串创建对象。
2 需要将 ObservableCollection 与数据网格绑定
所以 ObservableCollection 中的数据集作为行添加到数据网格中(有 五列)
谁能给我一个方法来实现这个目标?
编辑:这些是我实现的代码,
<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类属性应该放到数据网格的列中
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; }
}
当添加一个项目时,它被发送到 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);
在网格的 xaml 代码中,我已经直接绑定到数据库,我需要将其绑定到 ObservableCollection
【问题讨论】:
-
为什么不创建一个包含 5 列的数据网格并为此创建一个数据类。这样数据网格的源就可以绑定到您的视图模型中的 ObservableCollection
。您可以在数据网格的模板内设置每一列。在这种情况下,您无需将每个项目添加到列表中。也许将从数据库中获取的所有项目添加到 ObservableCollection -
@vishakh369 我没有尝试过,因为在我的应用程序中,一次数据网格将有 300 多行,所以创建对象会浪费内存,我想“只是为了显示”它。
-
这怎么会浪费内存呢?如果您的源列表绑定到目标,那么它将每次更新并添加一个新行。
-
@vishakh369 所以每次我必须在数据网格中显示行时,我都必须向 ObservableCollection 添加许多“数据类”对象,所以与只传递五个相比,这不会浪费内存字符串到 ObservableCollection 与传递数据类对象?
-
感谢@vishakh369,这是一个很大的帮助
标签: wpf binding datagrid observablecollection