J.Javan J.Javan - 1 month ago 14
C# Question

How to bind dynamic data to data grid in wpf?

I have the below class as my DataModel:

public class InspectorOutput
{
string m_SymbolName;
// Each string is a column name with its value in double
List<KeyValuePair<string, double>> m_ListPrices = new List<KeyValuePair<string, double>>();

public string SymbolName
{
get
{
return m_SymbolName;
}

set
{
m_SymbolName = value;
}
}

public List<KeyValuePair<string, double>> ListPrices
{
get
{
return m_ListPrices;
}

set
{
m_ListPrices = value;
}
}
public void addResult(string strResultName, double nResult)
{
ListPrices.Add(new KeyValuePair<string, double>(strResultName, nResult));
}
}


In my xaml window data grid is defined as below:

<DataGrid x:Name="gridStockData">
</DataGrid>


Later on, in my mainwindow I have the below code:

private void runProfile(object sender, RoutedEventArgs e)
{
ObservableCollection<InspectorOutput> listOutput = null;
Profile.Profile objProfile = null;
Inspector.InspectorManager objInspectorManager = null;
try
{
// Some code here which makes a profile out of user input in objProfile
objInspectorManager = new Inspector.InspectorManager();
// Calculate data based on given profile
listOutput = objInspectorManager.startInspector(objProfile);
// Show calculated data
gridStockData.ItemsSource = listOutput;
}
catch (Exception ex)
{
Logger.getInstance().error(ex.getStackTrace());
}
}


The problem is as follows:


  • I have stock data for 10 companies.

  • Each company has a symbol name.

  • Calculated data for each symbol is stored in m_ListPrices where each key is a column name and each value is a cell value


    • Note: Columns are not known until run time(ie: Based on user's selected algorithm column names and numbers may vary).




I have a calculator class that runs user selected algorithms. Each algorithm has it's own output which stores it in above data model.
How could I possibly bind this DataModel to a DataGrid in WPF?

Currently above code gives me the following output:

Grid Output

Answer

This is how I got over the problem: I create a DataTable as data source for my data grid which is filled with columns inside the dictionary.

                DataTable tbl = new DataTable();
            tbl.Columns.Add("Symbol Name", typeof(string));

            // Add columns
            foreach (InspectorOutput item in listScenarioOutput)
            {
                foreach (KeyValuePair<string, double> entry in item.ListPrices)
                {
                    tbl.Columns.Add(entry.Key, typeof(double));
                }
                break;
            }

            for (int i = 0; i < listScenarioOutput.Count; i++)
            {
                DataRow row = tbl.NewRow();
                row.SetField(0, listScenarioOutput[i].SymbolName);
                int j = 0;
                foreach (KeyValuePair<string, double> entry in listScenarioOutput[i].ListPrices)
                {
                    row.SetField(++j, entry.Value);
                }
                tbl.Rows.Add(row);
            }

            gridStockData.ItemsSource = tbl.DefaultView;
Comments