List> 如何绑定到datagrid中?

qq_23313131 2014-11-15 06:34:12
如果把 List<Dictionary<string, string>> 替换成List<Object>的话,数据可以出来,
List<Dictionary<string, string>> 要怎么才能绑定到dataGrid?

 binding = new System.Windows.Data.Binding("Age");
dataColumn.Binding = binding;

List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Id", "1");
dict.Add("Name", "tome");
dict.Add("Age", "25");
dictList.Add(dict);

dataGrid.ItemsSource = dictList;
...全文
637 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南小鱼 2014-11-15
  • 打赏
  • 举报
回复
引用 楼主 qq_23313131 的回复:
如果把 List<Dictionary<string, string>> 替换成List<Object>的话,数据可以出来, List<Dictionary<string, string>> 要怎么才能绑定到dataGrid?
 binding = new System.Windows.Data.Binding("Age");
            dataColumn.Binding = binding;

            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Id", "1");
            dict.Add("Name", "tome");
            dict.Add("Age", "25");
            dictList.Add(dict);

            dataGrid.ItemsSource = dictList;
没看出来代码有问题 不清楚ItemsSource是一个什么东东,换成DataSource
qq_23313131 2014-11-15
  • 打赏
  • 举报
回复
引用 1 楼 wangweimutou 的回复:
页面代码: <data:DataGrid x:Name="dataGrid"/> 数据字典:

            var list = new List<Dictionary<string, string>>();
            for (var i = 0; i < 5; i++)
            {
                var dict = new Dictionary<string, string> { { "苹果", (i * 5).ToString() }, { "香蕉", (i * 5).ToString() }, { "橘子", (i * 5).ToString() } };
                list.Add(dict);
            }
绑定数据:

            var first = list.First();
            var dataGrid = new DataGrid {AutoGenerateColumns = false};
            foreach (var node in first)
            {
                dataGrid.Columns.Add(new DataGridTextColumn { Header = node.Key, Binding = new Binding(string.Format("[{0}]",node.Key)) });
            }
            dataGrid.ItemsSource = list;
这样是新增加列,如何把dict list 里面的数据放到原来的列 我的列是动态生成的,
binding = new System.Windows.Data.Binding("Age");
            binding.Mode = System.Windows.Data.BindingMode.OneWay;
            DataGridTextColumn dataColumn = null;
            dataColumn = new DataGridTextColumn();
            dataColumn.Header = "Age";
wangweimutou 2014-11-15
  • 打赏
  • 举报
回复
页面代码: <data:DataGrid x:Name="dataGrid"/> 数据字典:

            var list = new List<Dictionary<string, string>>();
            for (var i = 0; i < 5; i++)
            {
                var dict = new Dictionary<string, string> { { "苹果", (i * 5).ToString() }, { "香蕉", (i * 5).ToString() }, { "橘子", (i * 5).ToString() } };
                list.Add(dict);
            }
绑定数据:

            var first = list.First();
            var dataGrid = new DataGrid {AutoGenerateColumns = false};
            foreach (var node in first)
            {
                dataGrid.Columns.Add(new DataGridTextColumn { Header = node.Key, Binding = new Binding(string.Format("[{0}]",node.Key)) });
            }
            dataGrid.ItemsSource = list;
  • 打赏
  • 举报
回复
ExpandoObject 方便于进行一些小的、简练的数据转换设计。在 Silverlight 等等与桌面.net框架兼容的网页客户端、手机客户端平台中也是支持的。 另外我在帖子中 http://bbs.csdn.net/topics/390863754 写了一个它与 json 格式转换的例子,可以参考。
  • 打赏
  • 举报
回复
如果使用 ExpandoObject,可以这样转换
public static class Extensions
{
    public static DataTable ConvertToTable(this IEnumerable<ExpandoObject> dictList)
    {
        var tbl = new DataTable();
        DataColumn col;
        DataRow row;
        IDictionary<string, object> dict;
        object value;
        foreach (var x in dictList)
        {
            row = tbl.NewRow();
            dict = (IDictionary<string, object>)x;
            foreach (var k in dict.Keys)
            {
                col = tbl.Columns[k];
                value = dict[k];
                if (col == null)
                    col = tbl.Columns.Add(k, value.GetType());
                row[col] = value;
            }
            tbl.Rows.Add(row);
        }
        return tbl;
    }
}
使用例如:
dynamic dict = new ExpandoObject();
dict.Id = 1;
dict.Name = "tome";
dict.Age = 25;
dict.XX = DateTime.Now;
dict.YY = dict.Age + 80;
var dictList = new List<ExpandoObject> { dict };
你可以看到,dict对象里边的属性可以是各种类型的,不一定只是string类型的。
qq_23313131 2014-11-15
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
你可以写一个转换
public static class Extensions
{
    public static DataTable ConvertToTable(this List<Dictionary<string, string>> dictList)
    {
        var tbl = new DataTable();
        DataColumn col;
        DataRow row;
        foreach (var x in dictList)
        {
            row = tbl.NewRow();
            foreach (var k in x.Keys)
            {
                col = tbl.Columns[k];
                if (col == null)
                    col = tbl.Columns.Add(k, typeof(string));
                row[col] = x[k];
            }
            tbl.Rows.Add(row);
        }
        return tbl;
    }
}
例如可以使用
List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Id", "1");
dict.Add("Name", "tome");
dict.Add("Age", "25");
dictList.Add(dict);
var table = dictList.ConvertToTable();
另外,我要告诉你,.net有一个专门做动态类型的 ExpandoObject 类,而且许多框架(例如MongoDB、NewtoneSoft.Net 等等)都直接支持它。你不需要自己发明车轮子,你发明的那个类的框架兼容性没有 ExpandoObject 类型那么好。。
好的,谢谢。
  • 打赏
  • 举报
回复
如果是在 WPF/Silverlight 中,而且是在 XAML 中声明的数据绑定,那么你自己写一个 IValueConverter 就行了,就能把你的那个类型的数据集合绑定给 DataGrid(Binding 表达式里声明Converter属性)。
  • 打赏
  • 举报
回复
你可以写一个转换
public static class Extensions
{
    public static DataTable ConvertToTable(this List<Dictionary<string, string>> dictList)
    {
        var tbl = new DataTable();
        DataColumn col;
        DataRow row;
        foreach (var x in dictList)
        {
            row = tbl.NewRow();
            foreach (var k in x.Keys)
            {
                col = tbl.Columns[k];
                if (col == null)
                    col = tbl.Columns.Add(k, typeof(string));
                row[col] = x[k];
            }
            tbl.Rows.Add(row);
        }
        return tbl;
    }
}
例如可以使用
List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Id", "1");
dict.Add("Name", "tome");
dict.Add("Age", "25");
dictList.Add(dict);
var table = dictList.ConvertToTable();
另外,我要告诉你,.net有一个专门做动态类型的 ExpandoObject 类,而且许多框架(例如MongoDB、NewtoneSoft.Net 等等)都直接支持它。你不需要自己发明车轮子,你发明的那个类的框架兼容性没有 ExpandoObject 类型那么好。。
  • 打赏
  • 举报
回复

Binding binding = new System.Windows.Data.Binding("Value");
//...省略
this.DataGridTest.ItemsSource = dictList[0];
List<Dictionary<string, string>> 你这样存储数据 看不出来有任何意义。 Dictionary的key 是不可能重复的,你再给添加到List里,有点多余了。

111,113

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧