C#WinForms - 使用序列化JSON对象手动添加Datagridview不显示数据

时间:2016-02-06 00:53:54

标签: c# json winforms datagridview

我已经回顾了关于SO和其他地方的100多篇帖子和文章,还没有找出为什么下面这个例子不会显示任何列或行。必须有一个简单的事情,我正在做的不正常或只是明显的错误导致它失败。

总的来说,我的目标是能够在将JSON文件拖放到WinForm后手动将数据网格添加到表单。但是,下面的示例只是将JSON序列化为对象的测试代码,然后查看是否可以在数据网格中显示数据。我在2015年使用C#WinForms。

到目前为止没有运气。我错过了什么?

SERIALIZED OBJECT     使用系统;     使用System.Runtime.Serialization;

public partial class Form1 : Form
{
    DataGridView datagrid = new DataGridView();

    public Form1()
    {
        InitializeComponent();

        DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Connections));
        String testData = "{ \"id\": 1, \"name\": \"testSetting\"}";
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(testData));
        Connections connection = (Connections)js.ReadObject(ms);
        ms.Close();      

        BindingSource binder = new BindingSource();
        binder.DataSource = typeof(Connections);
        binder.Add(connection);
        binder.Add(connection);
        binder.Add(connection);

        DataGridView datagrid = new DataGridView();
        datagrid.DataSource = binder;
        datagrid.AutoGenerateColumns = true;
        datagrid.AutoSize = true;

        this.Controls.Add(datagrid);
    }

FORM

null

0 个答案:

没有答案