DevD9
Спасибо. Вот как я решаю эту проблему.
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>