views:

45

answers:

3

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 best 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.

+1  A: 

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.

jrista
Thanks. Changed struct to class, no visible difference.
I. J. Kennedy
Can you please post your code? Include the generated code in the InitializeComponent() method that relates to the DataGridView as well. If you have it configured right, by default, binding to a list of classes should autogenerate columns and display information.
jrista
+1  A: 

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.

Andrew Keith
A: 

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.

shf301