Simple generic list as a datasource for a grid

C

cowznofsky

I'm getting data in a generic list class, which I'm not going to
change. I would like to
use it as a datasource for a datagrid or a gridview, but it doesn't
implement IEnumerable.

I'm wondering if there's a simple technique that would allow me to get
the data into the datagrid.
 
C

cowznofsky

One option is to use the generic BindingList<T> class.

http://msdn2.microsoft.com/en-us/library/ms132679.aspx#Mtps_DropDownF...







- Show quoted text -

Well, this looked to be just what I needed, but I haven't been able to
get it to work.
In the code below I have my original generic list, myColumns, and my
BindingList, bColumns. I populate bColumns, and I confirm that I can
get a value from the 'DB_Column_Name' property.
Then I try to populate a dropdownlist box. Perhaps I may be using the
wrong syntax here.
But I also try to populate a datagrid, and get an error saying that
'DB_Column_Name' is not found. If I try change the grid to
AutoGenerateColumns="True", it still does not work.

List<MasterColumnRecord> myColumns = wcc.Columns;

BindingList<MasterColumnRecord> bColumns = new
BindingList<MasterColumnRecord>();
foreach (MasterColumnRecord mcr in myColumns)
{
bColumns.Add(mcr);
}
string test = bColumns[0].DB_Column_Name;

SortList.DataSource = bColumns;
SortList.DataMember = "DB_Column_Name";
SortList.DataBind();

DataGrid1.DataSource = bColumns;
DataGrid1.DataBind();
 
C

cowznofsky

One option is to use the generic BindingList<T> class.
- Show quoted text -

Well, this looked to be just what I needed, but I haven't been able to
get it to work.
In the code below I have my original generic list, myColumns, and my
BindingList, bColumns. I populate bColumns, and I confirm that I can
get a value from the 'DB_Column_Name' property.
Then I try to populate a dropdownlist box. Perhaps I may be using the
wrong syntax here.
But I also try to populate a datagrid, and get an error saying that
'DB_Column_Name' is not found. If I try change the grid to
AutoGenerateColumns="True", it still does not work.

List<MasterColumnRecord> myColumns = wcc.Columns;

BindingList<MasterColumnRecord> bColumns = new
BindingList<MasterColumnRecord>();
foreach (MasterColumnRecord mcr in myColumns)
{
bColumns.Add(mcr);
}
string test = bColumns[0].DB_Column_Name;

SortList.DataSource = bColumns;
SortList.DataMember = "DB_Column_Name";
SortList.DataBind();

DataGrid1.DataSource = bColumns;
DataGrid1.DataBind();

Ok, I think I see part of my problem. "DB_Column_Name" was a public
varable in the class. When I make it a property, the datagrid bind
works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top