Hi Paul,
Based on my understanding, you'd like to show enum values of the SortOrder
in the drop down list of a DataGridViewComboBoxColumn and bind the
DataGridViewComboBoxColumn to a field of a DataTable in a DataSet. I'm off
base, please feel free to let me know.
You're right to set the DataSource property of the
DataGridViewComboBoxColumn to bind the column to a lookup data source. In
your scenario, the type of the data source is the SortOrder enum, so you
need to set the column's ValueType to the SortOrder type. The following is
a sample.
this.sortType.ValueType = typeof(SortOrder);
Then you could bind the column to any property of type SortOrder in a data
source. The following is a sample.
this.dataGridView1.DataSource = mylist;
// the property MyProperty is of type SortOrder
this.sortType.DataPropertyName = "MyProperty";
However, what you want is to bind the column to a DataTable, and the
columns in a DataTable can not be of any enum type. In this case, I suggest
that you create a class which has two properties, one for the int value of
an enum and the other for the enum value. Create a data source that
contains instances of the custom class and bind it to the
DataGridViewComboBoxColumn. Note that you needn't change the ValueType of
the column in this instance.
The following is the sample code.
class MyClass
{
int id;
SortOrder order;
public int ID
{
get { return id; }
set { id = value; }
}
public SortOrder Order
{
get { return order; }
set { order = value; }
}
public MyClass(int id, SortOrder order)
{
this.id = id;
this.order = order;
}
}
private void Form1_Load(object sender, EventArgs e)
{
Array arr =
Enum.GetValues(typeof(System.Windows.Forms.SortOrder));
List<MyClass> mylist = new List<MyClass>();
foreach (SortOrder ins in arr)
{
mylist.Add(new MyClass(Convert.ToInt32(ins), ins));
}
this.sortType.DataSource = mylist;
this.sortType.DisplayMember = "Order";
this.sortType.ValueMember = "ID";
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.DataSource = this.dataSet1;
this.dataGridView1.DataMember = "DataTable1";
this.sortType. DataPropertyName = "ColumnName";
}
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.