Monday, 11 July 2016

c# - DataGridView displays empty cells when bound to typed DataTable



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?


Answer




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.)


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...