I. J. Kennedy
I. J. Kennedy

Reputation: 25829

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

Upvotes: 0

Views: 448

Answers (3)

shf301
shf301

Reputation: 31404

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.

Upvotes: 0

Andrew Keith
Andrew Keith

Reputation: 7563

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.

Upvotes: 1

jrista
jrista

Reputation: 33000

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.

Upvotes: 1

Related Questions