ååãã¹ãã® Dapperの結果のDapperRowのコレクションを単純なDictionaryのコレクションに変換する - きよくらの備忘録 ã«é¢é£ãã¦ã
List<Dictionary<string, object>>
ã®å½¢ã®ãã¼ã¿ãWPFã®DataGridã«ãã¤ã³ãããã®ã«å°ãæéåã£ãã®ã§åå¿é²ã¨ãã¦ã
XAMLã§DataGridTextColumnãå®ç¾©ããå ´å
XAML:
<DataGrid xName="DataGrid" AutoGenerateColumns="False">
<DataGridColumns>
<DataGridTextColumn Header="ID" Binding="{Binding Item[ID]}" />
<DataGridTextColumn Header="Name" Binding="{Binding Item[Name]}" />
<DataGridTextColumn Header="ID" Binding="{Binding Item[BirthDay]}" />
</DataGridColumns>
</DataGrid>
C#ã³ã¼ã:
public MainWindow()
{
InitializeComponent();
var list = new List<Dictionary<string, object>>
{
new Dictionary<string, object>() {{"ID", 1}, {"Name", "Taro"}, {"BirthDay",new DateTime(2001,10,1)}},
new Dictionary<string, object>() {{"ID", 2}, {"Name", "Jiro"}, {"BirthDay",new DateTime(2004,2,3)}},
new Dictionary<string, object>() {{"ID", 3}, {"Name", "Saburo"}, {"BirthDay",new DateTime(2010,5,21)}}
};
DataGrid.ItemsSource = list;
}
å®è¡çµæä¾ï¼
åçã«DataGridTextColumnãå®ç¾©ããå ´å
ååã®ä¾ã®ããã«Keyãäºåã«XAMLã«è¨è¿°æ±ºå®ã§ããªããããªå ´åã¯DataGridTextColumn ãåçã«çæããã°OK
XAML:
<DataGrid xName="DataGrid" AutoGenerateColumns="False">
<DataGridColumns />
</DataGrid>
C#ã³ã¼ã:
public MainWindow()
{
InitializeComponent();
var list = new List<Dictionary<string, object>>
{
new Dictionary<string, object>() {{"ID", 1}, {"Name", "Taro"}, {"BirthDay",new DateTime(2001,10,1)}},
new Dictionary<string, object>() {{"ID", 2}, {"Name", "Jiro"}, {"BirthDay",new DateTime(2004,2,3)}},
new Dictionary<string, object>() {{"ID", 3}, {"Name", "Saburo"}, {"BirthDay",new DateTime(2010,5,21)}}
};
foreach (var key in list[0].Keys)
{
DataGrid.Columns.Add(new DataGridTextColumn()
{
Header = key,
Binding = new Binding($"Item[{key}]")
});
}
DataGrid.ItemsSource = list;
}