Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPFDataGridmongodb
Hello, I am working on one assignment using WPF and MongoDB. I need to generate WPF DataGrid(any other control will be fine if it works) at runtime from List of Dictionary<string,string>. This is simple collection Dictionary where each Dictionary has different set of keypairs.
 
So far i am able to generate columns at runtime from Dictionary<string,string> having highest number of columns. After generating all columns, task is to put data into DataGrid and display.
 
List of Dictionary<string,string> contains different set of columns in each dictionary when i tried using DataContext and ItemsSource of DataGrid but it generates empty gird.
 
Please provide your thoughts
Posted 26-Dec-12 3:35am
DevD9173
Edited 26-Dec-12 3:36am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello
I would first of all choose the ObservableCollection instead of the List.
That is because that list inherits the ICollectionChanged interface so that changes in the collection will appear in the UI when binding to the collection form the ItemSource property on the datagrid.
 
Second, I would not use a Dictionary to hold the data. I would create a class with properties for all the "keys" or "columns", that I create the ObservableCollection of.
 
When all this is done, I would use the MVVM model (my personal choice) but there are many alternatives. And I would use Binding in the XAML from the ItemSource to a property in the model holding the ObservableCollection of my values. That is a quick and easy way of solving the problem, I think.
 
Hope it helps!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well obviously
You might need to replace List<Dictionary<string,string>> to more WPF convenient collection ObservableCollection<IList<KeyValueHolder>> Items ,
where KeyValueHolder is siple entity , see:
 
public class KeyValueHolder
{
 public string Key {get;set;}
 public string Value {get;set;}
} 
.
 
But one moment to consider, by it's nature ObservableCollection is not Thraed-safe!!
 
So , in your scenario i think , would be prafarable to implements master-details approach,(you will have 2 datagrids: - first is general, represent number of your dictionaries, and second one is detailed by list of KeyValueHolder)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thanks. This is how i resolve it.
table = new DataTable();
            dgSearchResult.Columns.Clear();
            string broker = string.Empty;
            string producttype = string.Empty;
            string currency = string.Empty;
            string productsubtype = string.Empty;
 
            if (cmbProductTypes.SelectedValue != null)
                producttype = cmbProductTypes.SelectedValue.ToString();
            else
                producttype = "";
 
            if (cmbBrokers.SelectedValue != null)
                broker = cmbBrokers.SelectedValue.ToString();
            else
                broker = "";
 
            if (cmbProductSubtype.SelectedValue != null)
                productsubtype = cmbProductSubtype.SelectedValue.ToString();
            else
                productsubtype = "";
 
            if (cmbCurrency.SelectedValue != null)
                currency = cmbCurrency.SelectedValue.ToString();
            else
                currency = "";
//Sample is a list of dict of string string
            List<dictionary><string,string>> sample = Models.RateCards.GetCard(producttype,broker,productsubtype,currency);
 
            foreach (Dictionary<string,> d in sample)
            {
                foreach (string key in d.Keys)
                {
                    Addcolumn(key);
                }
 
            }
 
            foreach (Dictionary<string,> d in sample)
            {
                //Addrow(d);

                row = table.NewRow();
                foreach (KeyValuePair<string,> keyValue in d)
                {
                    if (table.Columns.Contains(keyValue.Key))
                    {
                        if (keyValue.Key != "XMLDefinition")
                        {
                            if (keyValue.Value.Contains(","))
                            {
                                //if (keyValue.Key == "Volume Band Rates" || keyValue.Key == "Premium Band Rates")
                                    row[keyValue.Key] = RemoveSpecialCharacters(keyValue.Value.Replace("}, ", "}" + System.Environment.NewLine).Replace(",", ";").Replace("{", "").Replace("}", "").Replace("[", "").Replace("]", "").Replace("''", ""));
                                //else
                                //    row[keyValue.Key] = RemoveSpecialCharacters(keyValue.Value.Replace(",", ";").Replace("{", "").Replace("}", "").Replace("[", "").Replace("]", "").Replace("''", ""));
                            }
                            else
                                row[keyValue.Key] = RemoveSpecialCharacters(keyValue.Value);
                        }
                        else
                            row[keyValue.Key] = keyValue.Value;
                    }
                }
 
                table.Rows.Add(row);
 
            }
            
            dgSearchResult.ItemsSource= table.DefaultView;
            
       }
 
       private static string RemoveSpecialCharacters(string str) 
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str) 
            {
                if (c != '"') 
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }
 
        private void Addcolumn(string columnname)
        {
            if (!table.Columns.Contains(columnname))
            {
 
                DataGridTextColumn dgColumn = new DataGridTextColumn();
                dgColumn.Header = columnname;
                dgColumn.Binding = new Binding(string.Format("[{0}]", columnname));
                dgColumn.SortMemberPath = columnname;
                dgColumn.IsReadOnly = true;
                
                dgSearchResult.Columns.Add(dgColumn);
                
                DataColumn dtcolumn = new DataColumn();
                dtcolumn.Caption = columnname;
                dtcolumn.ColumnName = columnname;
                
                table.Columns.Add(dtcolumn);
                if (columnname == "_id" || columnname == "XMLDefinition")
                    dgColumn.Visibility = Visibility.Hidden;
                    
            }
        }</dictionary>
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 498
2 Tadit Dash 420
3 sanket saxena 337
4 Zoltán Zörgő 230
0 Sergey Alexandrovich Kryukov 11,800
1 OriginalGriff 7,225
2 Peter Leow 5,009
3 Abhinav S 3,893
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid