Hi Bruno_1,
If you have two DataGridViewTextBoxColumns and want to bind each of them separately to a data source, there is no way to do this. You need to set DataGridView.VirtualMode property to true and populate the data by yourself.
However, your scenario is a bit special because the existence of a DataGridViewComboBoxColumn in your DataGridView control. You can set your DataGridView’s data source to your first data source and set DataGridViewComboBoxColumn’s data source to your second data source.
Your further requirement is difficult to implement. Once you bind the entire DataGridViewComboBoxColumn to one data source, all of the DataGridViewCombBoxCells are bind to that data source. Modification on the data source affects all the cells in that column. One way to walk around is to create a new ComboBoxCell and cover it on current DataGridViewComboBox being edited.
Code Snippet
partial class Form1 : Form
private BindingSource personsBindingSource = new BindingSource();
private BindingList<int> agesBindingList = new BindingList<int>(); private BindingList<int> promptedBindingList = new BindingList<int>();
private int oldValue = -1;
private void InitializeBindingSource()
typeof(Person);
new Person("Jacob", 10));
new Person("Johan", 20));
for (int i = 0; i < 10; i ++ )
promptedBindingList.Add(i);
private void Form1_Load(object sender, EventArgs e)
false;
DataGridViewTextBoxColumn firstTextBoxColumn = new DataGridViewTextBoxColumn();
"Name";
"Name";
"Name";
DataGridViewComboBoxColumn secondComboBoxColumn = new DataGridViewComboBoxColumn();
"Age";
"Age";
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == 1)
{
DataGridViewComboBoxCell cell = dataGridView1.CurrentCell as DataGridViewComboBoxCell;
if (cell.Value == null)
{
oldValue = -1;
}
else
{
oldValue = Int32.Parse(cell.Value.ToString());
} cell.Value = null;
cell.DataSource = promptedBindingList;
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
DataGridViewComboBoxCell cell = dataGridView1.CurrentCell as DataGridViewComboBoxCell;
if (cell.Value != null)
{
int selectedValue = Int32.Parse(cell.Value.ToString());
int index = promptedBindingList.IndexOf(selectedValue);
promptedBindingList.RemoveAt(index);
if (oldValue != -1)
{
promptedBindingList.Add(oldValue);
oldValue = -1;
} cell.DataSource = agesBindingList;
index = agesBindingList.IndexOf(selectedValue);
cell.Value = agesBindingList[index];
}
else
{
if (oldValue != -1)
{
promptedBindingList.Add(oldValue);
oldValue = -1;
}
cell.DataSource = agesBindingList;
}
}
}
Code Snippet
class Person
private String m_name;
private int m_weight;
public String Name
get
return m_name;
set
value;
public int Weight
get
return m_weight;
set
value;
public Person()
"";
public Person(String aName, int theWeight)
Best regards,
Jacob
|