简体   繁体   中英

Create a table in compact framework

I need to display some data into a table but i don't get wich class/object to do so in compact framework. The table doesn't have to do anything, just display the data. I have tried with datagrid like this:

DataGrid table = new DataGrid();
table.Location = new Point(13,190);
table.Size =  new Size(221,100);
List<int> list = new List<int>();
list.Add(5);
list.Add(7);
list.Add(9);
table.DataSource = list;
this.Controls.Add(table);

But this produce what seems to be an empty datagrid(one column, four rows and one row have an arrow).

The reason you get "empty" grid is:

To bind the DataGrid to a strongly typed array of objects, the object type must contain public properties

You use int as list type parameter, but int has no properies to display. Replace list type parameter with other type with public properties and you get what you want.

Here, try this:

   DataGrid table = new DataGrid();
   table.Location = new Point(0, 0);
   table.Size = new Size(221, 100);
   List<KeyValuePair<object, object>> list = new List<KeyValuePair<object, object>>();
   list.Add(new KeyValuePair<object, object>("asdfasdf", 3685745));
   list.Add(new KeyValuePair<object, object>("sdfgsdfgsd", 54));
   list.Add(new KeyValuePair<object, object>("xcvbxcvbxcvb", 341234));
   list.Add(new KeyValuePair<object, object>("56785678567", 56));
   table.DataSource = list;
   this.Controls.Add(table); 

If you need to display data like ints or strings, you can use linq do this:

table.DataSource = list.Select(item=>new {item}).ToList();

Instead of List<int> , try using a List of Objects, where the Object is a class you write to store your data.

Here's an example:

public class MyData {

  public MyData() {
    Date = DateTime.MinValue;
  }

  public int ID { get; set; }

  public string Text { get; set; }

  public DateTime Date { get; set; }

  public override ToString() {
    return string.Format("{0}: {1}", ID, Text);
  }

}

Now, use the MyData class to create your list and fill it with ...whatever you have:

var list = new List<MyData>();
list.Add(new MyData() { ID = 5, Text = "5", new DateTime(2012, 5, 1) });
list.Add(new MyData() { ID = 7, Text = "7", new DateTime(2012, 7, 1) });
list.Add(new MyData() { ID = 9, Text = "9", new DateTime(2012, 9, 1) });
table.DataSource = list;

And, FYI: the word table is not a good choice for a DataGrid instance. People on here will not know what you're talking about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM