views:

55

answers:

2

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)
        {
            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!

+1  A: 

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

}

msergeant
This didn't fix it, but what interested me is that you said that you saw something display with your old method - all I see is a blank grid.
adam_0
Did you do anything special when setting up the DataGridView? I just created it in the visual designer
adam_0
Is your code running exactly as you've posted it? If so, you might be missing a call to EndInit() on DataGridView.
msergeant
+1  A: 

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(); 
Mario