Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView, DataGridViewComboboxColumn and (important!) DataTable.
 

DataGridView, DataGridViewComboboxColumn and (important!) DataTable.

Hi, all!

I need to bind DataTable to DataGridView. DataTable contains column of enum type. I haveseen many samples of binding business object collection with enum property, but it dosn't work with DataTable!

Here is code sample (one file for all):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace DataGridViewTest
{

public partial class Form1 : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}


public Form1()
{
// Binding Source
BindingSource bindingSource = new BindingSource();

// DataTable
DataTable dt = new DataTable("Records");
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Kind", typeof(Kind));

// Grid
DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;
grid.DataSource = bindingSource;
Controls.Add(grid);

// Grid Columns
DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
nameColumn.DataPropertyName = "Name";

DataGridViewComboBoxColumn kindColumn = new DataGridViewComboBoxColumn();
kindColumn.DataPropertyName = "Kind";

grid.Columns.AddRange(new DataGridViewColumn[] { nameColumn, kindColumn });

// Lookup list, Kind -> (string) Kind display value
List<KeyValuePair<Kind, string>> lookup = new List<KeyValuePair<Kind, string>>();
foreach (Kind kind in Enum.GetValues(typeof(Kind)))
lookup.Add(new KeyValuePair<Kind, string>(kind, "Kind = " + kind.ToString()));

// ComboboxColumn dropdown tuning
kindColumn.DataSource = lookup;
kindColumn.ValueMember = "Key";
kindColumn.DisplayMember = "Value";

// Attach DataTable to BindingSource
bindingSource.DataSource = dt;

//
// Error appears after manual selection of Kind value in grid
// >>>>>>>>>>>>>>>>>BEGIN OF DIALOG

//---------------------------
//DataGridView Default Error Dialog
//---------------------------
//The following exception occurred in the DataGridView:

//System.FormatException: DataGridViewComboBoxCell value is not valid.

//To replace this default dialog please handle the DataError event.
//---------------------------
//OK
//---------------------------

// <<<<<<<<<<<<<< END OF DIALOG

// If data source is List<T> or array, not DataTable - error does not appears:
// UNCOMMENT TO VIEW:
//bindingSource.DataSource = new Record[]
// {
// new Record("Name 1", Kind.Kind1),
// new Record("Name 2", Kind.Kind2),
// new Record("Name 3", Kind.Kind3)
// };
}
}

public enum Kind
{
Kind1,
Kind2,
Kind3
}


public class Record
{
public Record(string name, Kind kind)
{
_name = name;
_kind = kind;
}

public Kind Kind
{
get { return _kind; }
set { _kind = value; }
}
private Kind _kind;

public string Name
{
get { return _name; }
set { _name = value; }
}
private string _name;
}

}

Igor Trofimov  Tuesday, February 06, 2007 7:00 AM
Hi Igor did you got a response to this one????
I need to do the same
Nicolas Rafalowski  Sunday, July 19, 2009 10:06 AM

You can use google to search for other answers

Custom Search

More Threads

• Databindings
• Datagridview does not commit changes made to DataTable in Vista
• DataGridView ComboBox Events
• VS.Net IDE - All Object Data Sources Gone
• Duplicate records in bindingsource following an edit.
• Losing datatype from dataset to datagridview & can't sort numerically
• DataGridView: is there a verbose method for determining ColumnIndex?
• Query/Databinding question
• problem with GridView in UpdatePanel after DropDownList postback
• DataSource and DataGridView problems. Couldn't resolve by googling. plz help