Windows Develop Bookmark and Share   
 index > Windows Forms General > Can I link List<T> to DataGridView?
 

Can I link List<T> to DataGridView?

DataGridView Control definitely can display structural data from db. However, I couldn't use db as the datasource, so I created the following List<T> and Struct in my WinForm.

As DataGridView supports different datasource, Is there a way to link my List<T> to the DataGridView? If not, what option is available if the only storage media is the PC memory, i.e. no db, no network connection to retrieve data remotely?

Also, is it possible whenever the List<T> content got changed, the DataGridView also refeshed automatically?

public partial class FormMain : Form

{

public List<fooStruct> l;

public struct fooStruct

{

public string title;

public int i;

public decimal d;

public bool state;

}

...

}

Thanks.

Wood-MSDN  Sunday, February 03, 2008 8:09 AM

Hi Wood-MSDN,

If you want when the data source content got changed, the DataGridView also refeshed automatically, you should use the BindingList instead. Also to force the data display in the DataGridView, you should define public properties instead of public fields. Here is a sample for your information.

Code Snippet

public partial class Form33 : Form

{

public Form33()

{

InitializeComponent();

}

BindingList<fooStruct> list = new BindingList<fooStruct>();

private void Form33_Load(object sender, EventArgs e)

{

for (int i = 1; i < 10; i++)

{

fooStruct myfoo = new fooStruct();

myfoo.Title = "Title" + i;

myfoo.State = (i % 2 == 0);

list.Add(myfoo);

}

this.dataGridView1.DataSource = list;

}

}

public struct fooStruct

{

private string title;

public string Title

{

get { return title; }

set { title = value; }

}

private bool state;

public bool State

{

get { return state; }

set { state = value; }

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, February 05, 2008 7:54 AM

Hi Wood-MSDN,

If you want when the data source content got changed, the DataGridView also refeshed automatically, you should use the BindingList instead. Also to force the data display in the DataGridView, you should define public properties instead of public fields. Here is a sample for your information.

Code Snippet

public partial class Form33 : Form

{

public Form33()

{

InitializeComponent();

}

BindingList<fooStruct> list = new BindingList<fooStruct>();

private void Form33_Load(object sender, EventArgs e)

{

for (int i = 1; i < 10; i++)

{

fooStruct myfoo = new fooStruct();

myfoo.Title = "Title" + i;

myfoo.State = (i % 2 == 0);

list.Add(myfoo);

}

this.dataGridView1.DataSource = list;

}

}

public struct fooStruct

{

private string title;

public string Title

{

get { return title; }

set { title = value; }

}

private bool state;

public bool State

{

get { return state; }

set { state = value; }

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, February 05, 2008 7:54 AM
Thanks, Rong-Chun. I will look into it...

Wood-MSDN  Tuesday, February 05, 2008 7:58 AM

You can use google to search for other answers

Custom Search

More Threads

• "Overriding" the Size field
• disabling SHIFT + DELETE
• Focus on a Window
• How can I increase form size in VB.Net?
• How to add scroll bar to form??
• Find greater number from given number
• manipulating richtextbox data
• timing methods
• Problem with displaying many Controls.
• Web browser control opens a new page in Internet explorer