Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a MyDataType object being returned as part of a datatable.  I have a MyDataTypeColumn type that will display both a string and image value from MyDataType (like the name column in explorer details view).

 

When I put the DataTable in as my DataGridView's DataSource it creates a DataGridViewTextBox column and populates it with the result of MyDataType.ToString() which isn't acceptable.  

 

How can I either force the autogenerated column to be a MyDataTypeColumn; or the DGV to use predefined columns instead of simply inserting new ones to the right of the existing ones when loading from its DataSource?

MyDataTable.Columns.Add("Symbol", typeof(MyDataType)); 
//finish creating columns and loading data.  This is all manual since my data isn't coming out of a database.
MyDataGridView.DataSource = MyDataTable;

Posted
Updated 17-Nov-09 3:19am
v2

1 solution


 You could set the AutoGenerateColumns prop on the DGV to false. Then You need to create the columns manually. Just make sure the DataPropertyName you specify matches the DataColumn name in the dataset.

This method also allows you specify the header text to be separate from the column name in the same method call.

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900