Reputation: 1837
I have made a code which makes few lists. In the end i get the following lists:
List<int> list_AmountNeed = new List<int>();
List<int> list_TotalCost = new List<int>();
List<int> list_TotalLose = new List<int>();
List<int> list_TotalGain = new List<int>();
after few calculations these lists contain values (24 in each if it matter):
while (z < list_Exp.Count)
{
list_AmountNeed.Add((goalexp - currentexp) / Convert.ToInt32(list_Exp[z]));
list_TotalLose.Add(list_AmountNeed[z] * (list_Amount_MadeFrom_One[z] * list_BuyPrice_MadeFrom_One[z] + list_Amount_MadeFrom_Two[z] * list_BuyPrice_MadeFrom_Two[z]));
list_TotalGain.Add(list_AmountNeed[z] * list_AmountMade[z] * list_SellPrice[z]);
list_TotalCost.Add(list_TotalGain[z] - list_TotalLose[z]);
z++;
}
Now, i have made a UI containing a button and Datagrid using blend and i want those lists to be shown in the Datagrid columns once i click the button.
what i did so far is inserting this code into the button xaml.cs. the thing im not sure how to do is if i can write the displaying code inside the button xaml.cs or it have to be in the datagrid and whats the right code to show it in columns:
column 1:
list_AmountNeed
column 2:
list_TotalCost
and so on.
Thank you!
Upvotes: 2
Views: 1271
Reputation: 35646
4 lists to store related data is a bad data structure (definitely not OOP). E.g. to add 1 piece of data ({amount,cost,lose,gain}) one have to perform 4 Add operations.
also DataGrid displays data row-wise so one cannot just go and display independent lists in different columns.
DataTable (suggested by @rory.ap) is an excellent general-purpose class and works fine with DataGrid but I would say it is too much for current requirement.
Let's instead create a POCO with 4 properties and a list of those objects with real values and display them in a DataGrid.
public class MyDataObject
{
public int Amount { get; set; }
public int Cost { get; set; }
public int Lose { get; set; }
public int Gain { get; set; }
}
window content xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Button Content="+" Click="DisplayClick"/>
<DataGrid Name="Dg" Grid.Row="1"/>
</Grid>
window code-behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private List<MyDataObject> L = new List<MyDataObject>
{
new MyDataObject {Amount = 1, Cost = 1, Gain = 1},
new MyDataObject {Amount = 2, Cost = 2, Gain = 4},
new MyDataObject {Amount = 3, Cost = 3, Gain = 9},
};
private void DisplayClick(object sender, RoutedEventArgs e)
{
Dg.ItemsSource = L;
}
}
When ItemsSource is assigned, DataGrid generates a column for each property (property name is displayed in a header) and create bindings between columns and properties
result:
if necessary extend MyDataObject with additional properties, e.g.
public class MyDataObject
{
public int SellPrice {get; set; }
public int Amount { get; set; }
public int Cost { get; set; }
public int Lose { get; set; }
public int Gain { get; set; }
}
calculation method can be modified to use MyDataObject objects
var L = new List<MyDataObject>();
for(int z = 0; z < list_Exp.Count; z++)
{
var d = new MyDataObject();
d.Amount = (goalexp - currentexp) / Convert.ToInt32(list_Exp[z]);
d.Lose = d.Amount * (list_Amount_MadeFrom_One[z] * list_BuyPrice_MadeFrom_One[z] + list_Amount_MadeFrom_Two[z] * list_BuyPrice_MadeFrom_Two[z]);
// d.SellPrice = list_SellPrice[z];
d.Gain = d.Amount * list_AmountMade[z] * list_SellPrice[z];
d.Cost = d.Gain - d.Lose;
L.Add(d);
}
Upvotes: 3
Reputation: 2317
Very similar to ASh, using the same principles. My datagrid is in a Winform, but you can perform the same operations on your XAML datagrid.
using System.Collections.Generic;
using System.Windows.Forms;
namespace PopulatingDataGridColumns_42584334
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<int> list_AmountNeed = new List<int>() { 3, 6, 7, 8, 9, 12 };
List<int> list_TotalCost = new List<int>() { 4, 3, 6, 9, 65, 87 };
List<int> list_TotalLose = new List<int>() { 7, 9, 2, 5, 15, 27 };
List<int> list_TotalGain = new List<int>() { 3, 1, 2, 2, 0, 4 };
List<gridviewdata> mydatalist = new List<gridviewdata>();
for (int i = 0; i < list_AmountNeed.Count; i++)
{
mydatalist.Add(new gridviewdata
{
col1 = list_AmountNeed[i].ToString(),
col2 = list_TotalCost[i].ToString(),
col3 = list_TotalLose[i].ToString(),
col4 = list_TotalGain[i].ToString()
});
}
dataGridView1.DataSource = mydatalist;//assuming the datagridview is named dataGridView1
}
}
class gridviewdata
{
public string col1 { get; set; }
public string col2 { get; set; }
public string col3 { get; set; }
public string col4 { get; set; }
}
}
Upvotes: 0