views:

422

answers:

1

I'm creating a simple application that simply populates a DataGridView with a strongly typed DataTable. My problem is that all the cells show up empty despite containing data, I have a CellDoubleClick event which can pick up the underlying value of the cell (which is typed as expected) but the column is shown as empty.

Objects of type INode all have a ToString() method defined and my assumption was that the DataGridView would be intelligent enough to call that method any show that as the cell contents but it appears not.

Here is my current test code:

        //Add some fake test data
        DataTable data = new DataTable();
        data.Columns.Add("Subject");
        data.Columns.Add("Predicate");
        data.Columns.Add("Object");
        data.Columns["Subject"].DataType = typeof(INode);
        data.Columns["Predicate"].DataType = typeof(INode);
        data.Columns["Object"].DataType = typeof(INode);

        Graph g = new Graph();
        DataRow row = data.NewRow();
        row["Subject"] = g.CreateURINode(new Uri("http://example.org/subject"));
        row["Predicate"] = g.CreateURINode(new Uri("http://example.org/predicate"));
        row["Object"] = g.CreateURINode(new Uri("http://example.org/object"));
        data.Rows.Add(row);
        this.dvwBrowser.DataSource = data;
        this.dvwBrowser.AutoResizeColumns();

How can I get the value of the ToString() method to be displayed in the cells when I bind the DataTable to the DataGridView?

A: 

dvwBrowser columns are using a user-defined type (INode) not one of the base .NET Framework data types and Byte[]. INode have to be marked by the SerializableAttribute at least. You'll find further explanations here (at the bottom of the page, before the code example.)

najmeddine
thanks for the answer, I decided just to create an unbound DataGridView and just stick the value of ToString() into the Grid and then just keep the DataTable as member variable of the form and reference into it as needed
RobV