2

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!

4
  • It sounds like what you have is a collection of lists where each datum at a given index in each list together logically represents a "data row" for you grid. This is a very round-about and probably not-too-workable way of doing things. I would suggest loading them into an existing data structure that's more suitable for this, such as a data table, then bind that to your grid. Either that, or create an object hierarchy (aggregation) and use object binding. Commented Mar 3, 2017 at 16:49
  • i thought about it, how ever when drawing a UI in blend which makes it looks very nice i can draw only datagrid and not table. Commented Mar 3, 2017 at 16:50
  • no, i'm not talking about not using the data grid, i'm talking about the data structure that you bind to the data grid. Check out msdn.microsoft.com/en-us/library/… Commented Mar 3, 2017 at 16:51
  • i checked this site. However, im not sure where this text should be put in. The xaml of the datagrid? the .cs of the data grid? because it cant find that there is a datagrid to put the data at. - @rory.ap Commented Mar 3, 2017 at 17:25

2 Answers 2

3

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:

datagrid with values


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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you mate, However, if the values of Amount for example arent =1 but they are a vector/array that has been calculated earlier in the code?
@ben, then perhaps you should include your existing classes code in the question. without any knowledge about them all I can do is give general advice about datagrid databinding
i edited the questions to make it more clear. Thank You.
is there a way to control that those columns will be at columns 3-6 in the grid because 1-2 are already binded with data from blend? @ASh
DataGrid can only have one ItemsSource. My suggestion in a nutshell is to use data class with all necessary properties or extend existing data class with additional properties
|
0

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; }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.