【问题标题】:Trying to create DataGridView from key-value pairs in C#尝试从 C# 中的键值对创建 DataGridView
【发布时间】:2010-07-01 17:05:57
【问题描述】:

在 Windows 窗体应用程序中,我试图创建一个包含两列的 DataGridView:一列用于 XML 元素给出的键,另一列用于所述 XML 元素的值。到目前为止,这是我的代码:

        this.myData = new DataGridView();
        ((System.ComponentModel.ISupportInitialize)(myData)).BeginInit();

        myData.Location = new System.Drawing.Point(12, 42);
        myData.Name = "myData";
        myData.Size = new System.Drawing.Size(1060, 585);
        myData.TabIndex = 32;

        foreach (XElement xElem in xInfoItems)
        {
            numItems++;
        }

        myData.Columns.Add(new DataGridViewTextBoxColumn());
        myData.Columns.Add(new DataGridViewTextBoxColumn());
        myData.Columns[0].Name = "Key";
        myData.Columns[0].DataPropertyName = "key";
        myData.Columns[1].Name = "Value";
        myData.Columns[1].DataPropertyName = "value";

        List<myRow> data = new List<myRow>();


        foreach (XElement xElem in xInfoItems)
        {
            data.Add(new myRow(xElem.Attribute("key").Value, xElem.Value));
        }

        myData.DataSource = data;

        myData.Refresh();

        this.PerformLayout();

我已经确认data 中的所有信息都是通过 foreach 加载的,因此该部分可以正常工作。我的问题是网格显示,但网格上没有显示任何内容。我究竟做错了什么?我对这种数据类型不是很好,所以如果它很明显,我很抱歉。

更新

我发现我没有在设计视图中正确设置 myData。添加 myRow 类后,它运行良好。感谢您的帮助!

【问题讨论】:

    标签: c# xml winforms datagridview linq-to-xml


    【解决方案1】:

    问题可能出在您的 myRow 类中。当我试图重现您的代码时,我首先将“key”和“value”定义为 myRow 类的公共字段:

    public class myRow {
      public string key;
      public string value;
    
      public myRow( string Key, string Value )
      {
         key = Key;
         value = Value;
      }
    
    }
    

    这导致显示绑定的行,但文本不在单元格中。当我将它们都更改为属性时,绑定工作得更好:

    public class myRow{
      private string _key;
      public string key
      {
         get
         {
            return _key;
         }
      }
    
      private string _value;
      public string value
      {
         get
         {
            return _value;
         }
      }
    
      public myRow( string Key, string Value )
      {
         _key = Key;
         _value = Value;
      }
    

    }

    【讨论】:

    • 这并没有解决它,但我感兴趣的是你说你看到用你的旧方法显示的东西 - 我看到的只是一个空白网格。
    • 你在设置DataGridView的时候有没有做过什么特别的事情?我刚刚在视觉设计器中创建了它
    • 您的代码是否完全按照您发布的方式运行?如果是这样,您可能会错过对 DataGridView 上的 EndInit() 的调用。
    【解决方案2】:

    我对您的代码所做的修改可能会有所帮助。 (我只关注使用 DataTable 创建列和添加行的部分)。

    this.myData = new DataGridView();
    ((System.ComponentModel.ISupportInitialize)(myData)).BeginInit();
    
    myData.Location = new System.Drawing.Point(12, 42);
    myData.Name = "myData";
    myData.Size = new System.Drawing.Size(1060, 585);
    myData.TabIndex = 32;
    
    foreach (XElement xElem in xInfoItems)
    {
        numItems++;
    }
    
    
    // Here we create a DataTable with two columns.
    DataTable table = new DataTable();
    table.Columns.Add("Key", typeof(string));
    table.Columns.Add("Value", typeof(string));
    
    foreach (XElement xElem in xInfoItems)
    {
         //Here we add rows to table
         table.Rows.Add(xElem.Attribute("key").Value, xElem.Value);
    }
    
    myData.DataSource = table;
    
    myData.Refresh();
    
    this.PerformLayout(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-24
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2016-03-01
      相关资源
      最近更新 更多