Click to See Complete Forum and Search --> : How to customize the columns of a DataGrid that bases on a IList?


Atlantis11500
September 26th, 2006, 03:45 AM
For example, I have a couple of Student records and want to show them by a DataGrid.

Student:
DWORD ID;
string strName;
string strAddress,
string strEmail.

So, I create a List of Students and bind it with a DataGrid as follows.

//Create a list of Students.
IList students = new ArryList();
students.add(new Student(1, "steven"......));
student.add(new Student(2, "Tom"......));
......

//Bind DataGrid to these data source.
myDataGrid.DataSource = students;

Now, I can get a DataGrid which has four columns...say, ID, Name, Email, Address... but, however, what I need to do is to customize these columns, for example, I only want to show two columns, Name and Email...or,even i want to show the Email column before/after Name...or somthing else.

What should I do? Must I inherite from the DataGrid and rewrite my own DataGrid?

torrud
September 26th, 2006, 04:26 AM
You have to use DataGridTableStyles and DataGridColumnStyles. But maybe you should not use simply an ArrayList. Because you have to set the MappingName to ArrayList and so your DataGrid will use that Style for every ArrayList. It is better to use customized collections. You have to set the MappingName property to your collection class name.

jhammer
September 26th, 2006, 05:20 AM
How to use DataGridColumnStyles programatically (http://j1hammer.blogspot.com/2005/10/how-to-use-datagridcolumnstyles.html)

In this example I used DataTable instead of a collection, but it should explain how to do what you need.