开发者

Trying to create DataGridView from key-value pairs in C#

开发者 https://www.devze.com 2023-01-05 22:26 出处:网络
In a Windows Form application, I\'m trying to create a DataGridView with two columns: one for the key given by an XML element and one for the value of said XML element. This is my code so far:

In a Windows Form application, I'm trying to create a DataGridView with two columns: one for the key given by an XML element and one for the value of said XML element. This is my code so far:

        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开发者_如何学Python)
        {
            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();

I have confirmed that all of the information in data is being loaded via foreach, so that part is working. My problem is that the grid displays, but nothing shows up on the grid. What am I doing wrong? I'm not very good with this data type so I apologize if it's something obvious.

UPDATE

I figured out that I hadn't set myData up properly in the Design view. After adding the myRow class, it worked perfectly. Thanks for the help!


The problem may lie in your myRow class. When I was trying to reproduce your code, I first defined "key" and "value" as public fields of the myRow class as so:

public class myRow {
  public string key;
  public string value;

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

}

This caused the bound rows to show up but the text was not in the cells. When I changed both of them to properties, the binding worked much better:

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;
  }

}


Probably the modifications I made on your code can help. (I just have focused on the part where you create the columns and add the rows, using a 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(); 
0

精彩评论

暂无评论...
验证码 换一张
取 消