Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

c# - WPF: Dictionary<int, List<string>> in DataGrid

I have a Dictionary<int, List<string>>. Each ID(int) has a corresponding dynamic list of names(List<string>).

This is the expected output in Datagrid.

ID | Name | Name | Name
1    Ash    Tina   Kara
2    Kc     
3    Star   Lara

How do I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 <DataGrid x:Name="dg" ItemsSource="{Binding Dic}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="id" Binding="{Binding Key}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Value[0]}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Value[1]}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Value[2]}"/>
            </DataGrid.Columns>
        </DataGrid>

enter image description here

if Name is not a fixed data, you need add column dynamicly like this:

 DataGridTextColumn column = new DataGridTextColumn();
            column.Header = "name4";
            column.Binding = new Binding("Value[3]");
            dg.Columns.Add(column);

all right ,here is my code:

 <DataGrid x:Name="dg" ItemsSource="{Binding Dic}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="id" Binding="{Binding Key}"/>
            </DataGrid.Columns>
        </DataGrid>


private Dictionary<int, List<string>> dic;

        public Dictionary<int, List<string>> Dic
        {
            get { return dic; }
            set { dic = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            Dic = new Dictionary<int, List<string>>();
            Dic.Add(1, new List<string> { "a", "b", "c", "5" });
            Dic.Add(2, new List<string> { "d" });
            Dic.Add(3, new List<string> { "e", "f" });

            int count = 0;
            foreach (List<string> lst in Dic.Values)
            {
                if (lst.Count > count)
                {

                    for (int i = count; i < lst.Count; i++)
                    {
                        DataGridTextColumn column = new DataGridTextColumn();
                        column.Header = "name" + i;
                        column.Binding = new Binding(string.Format("Value[{0}]", i));
                        dg.Columns.Add(column);
                    }
                    count = lst.Count;
                }
            }


        }

but i'd like you finish it by yourself


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...