开发者

Small, editable table of strings. Which Forms control do I want? (.NET)

I have a small array of structs, each struct has three fields, all strings. I want to display these structs in a grid, let the user edit the strings a la Excel, and then retrieve the edited strings of course. Which WinForms control is bes开发者_运维知识库t for this?

Tried a DataGridView but setting the DataSource to the array of structs doesn't seem to work. There are myriad controls with similar names but I can't figure out what does what. All the examples I've found are geared toward using a database as the data source--I just have a simple array.


You really are not gaining much by having a struct full of strings. That simply pushes a 3-pointer chunk on the the stack, and the pointers point to the heap. You are better off using a class, as you will consume less stack space and be more efficient in the end, after which you should be able to bind the DataGridView fine.


have you tried a ListView ?

You can add the strings like this.

foreach(Data d in datas)
{
  ListViewItem item =new ListViewItem(d.first);
 item.SubItems.Add(d.second);
item.SubItems.Add(d.third);

listview.Items.Add(item);
}

There is some option to do inline editing of strings. I cant remember which option it is.Then after the user edits, just read the values back.


The problem is that data binding only works with properties not fields. I'm assuming your class looks like:

class Strings {
    public string S1;
    public string S2;
    public string S3;
}

Change the public fields to properties like, for example

class Strings {
    public string S1 { get; set; }
    public string S2 { get; set; }
    public string S3 { get; set; }
}

And you should find that you can data bind your array to the DataGridView.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜