Bind Combobox with enum values to a datagridview

G

Guest

I'm trying to duplicate the functional the Query Designer functionality from
the SQL Server Management Studio. To do that I populate a datagridview with a
dataset containing the columns to select from. My problem is the Sort Type
combobox. How do I implement that ?

private System.Windows.Forms.DataGridViewComboBoxColumn sortType;
sortType.DataSource = Enum.GetValues(typeof(System.Windows.Forms.SortOrder))

what values should the relevant properties of the sortType combobox have and
how do I bind them to the SortType property on the rows in the dataset ?


Thanks
Paul S
 
L

Linda Liu [MSFT]

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

Guest

Thanks a lot, Linda
I just thought there was some smart way to avoid the construction with
MyClass, since the enum contains all the information needed.

- your solution works.
 
G

Guest

I found this more simple solution if you're willing to pay with performance
because of the casting:

using (List<DictionaryEntry> myList = new List<DictionaryEntry>())
{
foreach (System.Windows.Forms.SortOrder so in
Enum.GetValues(typeof(System.Windows.Forms.SortOrder)))
{
types.Add(new DictionaryEntry(so.ToString("D"),
so.ToString("G")));
}
this.sortType.DataSource = myList;
this.sortType.ValueMember = "Key";
this.sortType.DisplayMember = "Value";
}
 
L

Linda Liu [MSFT]

Hi Paul,

Thank you for sharing your new solution with us!

Yes, I think this solution is more simple : )


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top