I have a DataGridView with a ComboBox Column, binded to a bindingsource. The combobox are filled each with different list of Item. I'm having now an issue, since the value I select in the combo get lost as soon as i change the grid row. If i select again the row, the value is displayed back.
Each combo in the grid is populated using different list of items. I found that the issue is related to the fact that the value i select does not exist in the other combobox.
Any help will be very very appreciated, since i am not able to find a solution. Following you ca find a simple example to reproduce my issue:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<TestDataSource> ds = new List<TestDataSource>();
TestDataSource i = new TestDataSource();
i.Text = "Text1";
IDValore iv = new IDValore();
iv.id = "id1";
iv.val = "val1";
IDValore iv2 = new IDValore();
iv2.id = "id2";
iv2.val = "val2";
i.ValueList.Add(iv);
i.ValueList.Add(iv2);
TestDataSource i2 = new TestDataSource();
i2.Text = "Text2";
IDValore iv3 = new IDValore();
iv3.id = "id1";
iv3.val = "val1";
IDValore iv4 = new IDValore();
iv4.id = "id4";
iv4.val = "val4";
i2.ValueList.Add(iv3);
i2.ValueList.Add(iv4);
ds.Add(i);
ds.Add(i2);
testDataSourceBindingSource.DataSource = ds;
}
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
}
}
public class TestDataSource
{
public String Text { get; set; }
public List<IDValore> ValueList { get; set; }
public TestDataSource()
{
Text = String.Empty;
ValueList = new List<IDValore>();
}
}
public class IDValore
{
public String id { get; set; }
public String val { get; set; }
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.valoriBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.testDataSourceBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.textDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ComboColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.valoriBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.testDataSourceBindingSource)).BeginInit();
this.SuspendLayout();
//
// valoriBindingSource
//
this.valoriBindingSource.DataMember = "ValueList";
this.valoriBindingSource.DataSource = this.testDataSourceBindingSource;
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.textDataGridViewTextBoxColumn,
this.ComboColumn});
this.dataGridView1.DataSource = this.testDataSourceBindingSource;
this.dataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
this.dataGridView1.Location = new System.Drawing.Point(12, 26);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(345, 143);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView1_DataError);
//
// testDataSourceBindingSource
//
this.testDataSourceBindingSource.DataSource = typeof(UnitTest.TestDataSource);
//
// textDataGridViewTextBoxColumn
//
this.textDataGridViewTextBoxColumn.DataPropertyName = "Text";
this.textDataGridViewTextBoxColumn.HeaderText = "Text";
this.textDataGridViewTextBoxColumn.Name = "textDataGridViewTextBoxColumn";
this.textDataGridViewTextBoxColumn.ReadOnly = true;
this.textDataGridViewTextBoxColumn.Width = 150;
//
// ComboColumn
//
this.ComboColumn.DataSource = this.valoriBindingSource;
this.ComboColumn.DisplayMember = "val";
this.ComboColumn.HeaderText = "ComboColumn";
this.ComboColumn.Name = "ComboColumn";
this.ComboColumn.ValueMember = "id";
this.ComboColumn.Width = 150;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1026, 573);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.valoriBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.testDataSourceBindingSource)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.BindingSource valoriBindingSource;
private System.Windows.Forms.BindingSource testDataSourceBindingSource;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn textDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewComboBoxColumn ComboColumn;