-1

I have a List<Dictionary<string,string>> object (the Keys in the Dict in each list object are the same), and I want to convert it to a List<object>, which can be binded to a Datagrid with manually generate columns. This will make each Dictionary<string,string> in the original list object be a row in the datagrid, and the properties on the object expected to bind are the Keys in the Dictionary.

Many thanks,

Wei


I think your issue is with these lines:

var totship = ($("#totship").val() * 1);
var tottax = ($("#tottax").val() * 1);

The .val() method in jQuery has a tendency to return Strings. But if you multiply a String by a Number, you get NaN. You should force the values to be numbers. This seems to be what you're trying to do with * 1, but that's not going to work. Give this a try instead:

var totship = +$("#totship").val();
var tottax = +$("#tottax").val();

Or if clarity is your thing, you can do:

var totship = Number($("#totship").val());
var tottax = Number($("#tottax").val());

Or

var totship = parseInt($("#totship").val(), 10);
var tottax = parseInt($("#tottax").val(), 10);

Take your pick.

4

2 回答 2

-1

我认为对您来说最简单的方法是创建一个表并像这样填充它:让我们假设名为 lst 的列表

DataTable table = new DataTable();

foreach (var col in lst[0].Keys)
{
     table.Columns.Add(col, typeof(string));
}

foreach (var dict in lst)
{
     var row = table.NewRow();

     foreach (var data in dic)
     {
         row[data.Key] = data.Value;
     }

     table.Rows.Add(row);
}

现在剩下要做的就是将该表绑定到 DataGrid!

于 2013-05-12T12:24:14.923 回答
-1

做类似的事情

   <ListView Name="selectedPeople"
                  Width="480"
                  Height="200"
                  HorizontalAlignment="Right"
                  VerticalAlignment="Top"
                  ItemsSource="{Binding Path=Maps,
                                        RelativeSource={RelativeSource AncestorType=Window},
                                        Mode=OneWay}">
            <ListView.View>
                <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Broadcast call targets">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Key}" Header="ID" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Value}" Header="Description" />
                    <GridViewColumn Header="">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content=" X " IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>





  public partial class Listviewsamples : Window
    {
        public Listviewsamples()
        {
            InitializeComponent();

            Maps = new ObservableCollection<Dictionary<string, string>>();

            for (int i = 0; i < 10; i++)
            {
                Dictionary<string, string> item = new Dictionary<string, string>();
                item.Add(i.ToString(), " Item " + i.ToString());
                Maps.Add(item);
            }
            this.DataContext = this;
        }
        public ObservableCollection<Dictionary<string, string>> Maps { get; set; }


    }
于 2012-09-06T14:51:09.510 回答