TheGasMan

Hello,

I'm hoping someone can help me out here.

I want to get started using DataGridView, but I've not actually managed to find any examples that fit what I'm trying to do.

What I want to do is very simple:

(1) I have a list of class instances thus:

List<MyClass> classList = new List<MyClass>();

classList.Add(new MyClass("Fred"));
classList.Add(new MyClass("Wilma"));

(2) I want each MyClass member of classList to be represented as a row in my DataGridView

(3) I want to show only a subset of the class parameters, for example:


public class MyClass
{
private string name;
private int anumber;

// Want to show this in the interface
public String Name
{
get { return (name); }
set { name = value; }
}

// I DON'T want to show this in the interface
public int Number
{
get { return (anumber); }
set { anumber= value; }
}
}



I think that sums it up. No SQL, nothing just a nice list of data that I want to display.

Can anyone help Many thanks. I've attempted to find what I want from the FAQ, but I'm finding it hard to see the wood for the trees.. If I had a small example, I think I'd be better placed to understand what I need.

Many thanks!




Re: Windows Forms Data Controls and Databinding DataGridView - I need a simple example that works with List<??>

Yu Guo – MSFT

Hi, TheGasMan,

Based on my understanding, you want to hide a public property in DataGridView, don't you

I think there maybe 3 solutions for you.

1) Set the Browsable attribute to false;

Code Block

[Browsable(false)]

public int Number

{

get { return (anumber); }

set { anumber = value; }

}

The site effect is this property will not appear in PropertyGrid.

2) Set Visible of DataGridViewColumn to false

Code Block

dataGridView1.DataSource = classList;

dataGridView1.Columns["Number"].Visible = false;

3) Manually add DataGridViewColumn to DataGridView

Code Block

dataGridView1.AutoGenerateColumns = false;

DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();

name.HeaderText = "Name";

name.DataPropertyName = "Name";

dataGridView1.Columns.Add(name);

dataGridView1.DataSource = classList;

Hope this helps,

Regards