私はグーグルで答えを見つけましたが、見つけられませんでした。ここで は私の問題ですが、私は7列 データベースとデータグリッドをObservableCollectionにバインドする方法WPF
1データベース
でのObservableCollectionバインドする必要が付き- データベース・テーブル・コールsold_item_details
- データグリッドを持っています
私は弦のセットを取得したいfrデータベースを追加し、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");
同様にデータセットがあります。今まで私はObjectが追加されていることを見てきましたが、 これが達成されましたが、私は各文字列のオブジェクトを作成したくありません。
2のObservableCollection内部のデータセットので、データグリッドで
をのObservableCollectionバインドする必要がデータグリッドに行として追加されます( 5つの列があります)
誰も私にこれを達成するための方法を提案することができます?
EDIT:は、これらの項目を追加するときにデータグリッド
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; } }
の列に行くべきか、私は、このクラスの
<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クラスのプロパティを達成したコードです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にDataGridのソースをバインドすることができます。データグリッドのテンプレート内の各列を設定できます。この場合、各項目をリストに追加する必要はありません。たぶんデータベースからフェッチされたすべてのアイテムをObservableCollectionに追加する –
ViVi
@ vishakh369私のアプリケーションでは、データグリッドのデータグリッドが一度に300行以上になるため、オブジェクトを作成するメモリが浪費されます。私はそれを表示するだけです。 –
これはどのようにしてメモリの浪費になるでしょうか?ソースリストがターゲットにバインドされている場合は、毎回更新され、新しい行が追加されます。 – ViVi