DataGrid, DataSource, and ArrayList.

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
When setting the DataSource of a DataGrid to an ArrayList of objects, it automatically displays all properties of those classes. ie; If you had a Customer class that had two properties, Name and ID, they would be shown in the DataGrid.

My question is, how can I limit the properties that the DataGrid uses from its DataSource? Using the example above, say I only want to display the Name property from the Customer, and leave the ID out. The only solution I've been able to come up with is to turn the ID property into a method (ie; GetID()). Surely that's a valid solution, especially if the property is read-only anyway, but that solution may not always be applicable. If the Customer object grows into something more complex, and has the address and other info. available through properties, I can't just start redesigning all of my classes based on what DataGrid will and will not display.

However, another solution that presents itself is to create view classes which encapsulate the Customer object, CustomerView. Then only use that class for the DataGrid dispaly, and have the CustomerView class only provide properties which will be viewed in the DataGrid. But that isn't very flexible, and there may be situations where I want different customer data viewed at different times. Creating several different CustomerView classes just for this purpose doesn't sit well.

In any case, help would be appreciated. Thanks in advance.

EDIT:
Woops wrong forum, please move to Windows Forms. Thanks.
 
My advice would be to not let how you display the data drive how you code the data. Design your classes in a way that lets your represent the problem domain you are working in. Viewer classes may work. I would be inclined to drop the datagrid. It seems like that control doesn't really meet your needs for this particular problem.

Actually, the more I think about it, the view class might work. It would be a form side class that is a subset of the class it is representing. If nothing else, give it a try and let me know how it went. This is also a problem with the property viewer control...really slick, but gives way too much info for a user interface.
 
I was mainly looking at the DataGrid because it offered an easy way to display data (just link the ArrayList to the DataSource). It's also similar to an Excel sheet, where you can just click a cell and edit. ListView is nice and solid, but I have to do an extra few steps to display my data, but it would give me more control over how it's displayed. The only thing about the ListView is that you can't just click on a cell and edit it, it works much like a typical windows icon and how you rename that (click it once to select, then again and 1 second later it lets you edit it). Maybe there's a way to change how that works.
 
wyrd:
You just need to create a DataGridTableStyle and set its MappingName property to "ArrayList". Then create a ColumnStyle for each of the public properties in the object and use the ColumnStyle to control the layout of the public data (e.g. Width = 0)

Even though this will work, I agree with mskeel that your data objects should be designed so that you don't have to wrestle with this issue in the presentation.

Code:
private void frmGridArrayListTest_Load(object sender, System.EventArgs e)
{
    ArrayList arrayTest = new ArrayList();
    for(int count = 0; count < 10; count++)
    {
        TestObject test = new TestObject(count, "Name_" + count.ToString(), "Name2_" + count.ToString());
        arrayTest.Add(test);
    }

    DataGridTableStyle style = new DataGridTableStyle();
    style.MappingName = "ArrayList";
			
    DataGridTextBoxColumn colID = new DataGridTextBoxColumn();
    colID.MappingName = "ID";
    colID.Width = 0;
    style.GridColumnStyles.Add(colID);

    DataGridTextBoxColumn colName = new DataGridTextBoxColumn();
    colName.MappingName = "Name";
    colName.Width = 200;
    style.GridColumnStyles.Add(colName);

    DataGridTextBoxColumn colName2 = new DataGridTextBoxColumn();
    colName2.MappingName = "Name2";
    colName2.Width = 0;
    style.GridColumnStyles.Add(colName2);

    style.DataGrid = this.dataGrid1;
    this.dataGrid1.TableStyles.Add(style);

    this.dataGrid1.DataSource = arrayTest;
}

private class TestObject
{
    private int id;
    private string name;
    private string name2;

    public int ID
    {
        get
        {
	return this.id;
        }
    }

    public string Name
    {
        get
        {
            return this.name;
        }
    }

    public string Name2
    {
        get
        {
	return this.name2;
        }
    }


    public TestObject(int itemID, string itemName, string itemName2)
    {
        this.id = itemID;
        this.name = itemName;
        this.name2 = itemName2;
    }
}
 
Last edited:
Back
Top