ComboBox databinding and update

G

Guest

I have a few combobox's that are bound to a DataTable. The only problem I have is that if the field that is used in DisplayMember is modified, the comboBox contents are not updated to reflect the changes. Yet if I add or delete a DataRow the contents are updated. What gives?

The DataTable and DataRow objects are actually derived classes!
 
A

Alex Feinman [MVP]

Data changes cause updates because DataView implements IBindingList that
supports change notifications. How do you modify the contents of the
DisplayMember field?

--
Alex Feinman
---
Visit http://www.opennetcf.org
Brian said:
I have a few combobox's that are bound to a DataTable. The only problem I
have is that if the field that is used in DisplayMember is modified, the
comboBox contents are not updated to reflect the changes. Yet if I add or
delete a DataRow the contents are updated. What gives?
 
B

Brian Jones

To set up databinding I set the DataSource, DisplayMember and ValueMember
fields for the comboBox. For this case its a 'Machine' table and the
DisplayMember is the 'Name' field, ValueMember is the 'ID' field (again the
datasource is based on a DataTable). If I Add or Delete a row in the
DataTable the combo reflects the additions/deletions just like we expect.
However, if I change the 'Name' field in an existing row in the DataTable,
the combobox does not reflect that change.

This can't be normal or 'as designed' behavior, can it?

OK more information.. The 'DataTable' (MachinesDataTable) was developed by
another guy using the IDE for a regular framework project. Then modified to
work on the compact framework. Could this modification have affected the
way changes are reported to the ComboBox? He did say something about the
MachineDataTableChangeEvent code... I will look at this code later and
update the newsgroup.

Great job on the OpenNETCF project! I have learned more from it than any
other site. Although the www.codeproject.com site is awesome too!
 
B

Brian Jones

I took a look at the data access code... Looks ok to me. So I decided
to create a small app to test. The combo box will NOT update the
field if the field changes. (At least based on a DataTable) Please
remind me of why I abandoned Data Binding years ago.... Never mind, I
remember!!

Heres the code...

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace BindTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox1;
DataTable myTable;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
CreateNewDataRow();

DataRow myRow;
myRow = myTable.NewRow();
// Then add the new row to the collection.
myRow["fName"] = "Brian";
myRow["lName"] = "Jones";
myTable.Rows.Add(myRow);

myRow = myTable.NewRow();
// Then add the new row to the collection.
myRow["fName"] = "Frank";
myRow["lName"] = "Lombardo";
myTable.Rows.Add(myRow);

comboBox1.DataSource = myTable;
comboBox1.DisplayMember = "Fname";
comboBox1.ValueMember = "id";

textBox1.DataBindings.Add("Text", myTable, "Fname");

}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#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.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(56, 40);
this.comboBox1.Size = new System.Drawing.Size(200, 22);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(56, 80);
this.textBox1.Size = new System.Drawing.Size(120, 22);
this.textBox1.Text = "textBox1";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(266, 127);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.comboBox1);
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}

private void CreateNewDataRow(){
// Use the MakeTable function below to create a new table.
myTable = MakeNamesTable();
// Once a table has been created, use the NewRow to create a
DataRow.
DataRow myRow;
myRow = myTable.NewRow();
// Then add the new row to the collection.
myRow["fName"] = "John";
myRow["lName"] = "Smith";
myTable.Rows.Add(myRow);

// foreach(DataColumn dc in myTable.Columns)
// Console.WriteLine(dc.ColumnName);
// dataGrid1.DataSource=myTable;
}

private DataTable MakeNamesTable(){
// Create a new DataTable titled 'Names.'
DataTable namesTable = new DataTable("Names");
// Add three column objects to the table.
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "id";
idColumn.AutoIncrement = true;
namesTable.Columns.Add(idColumn);
DataColumn fNameColumn = new DataColumn();
fNameColumn.DataType = System.Type.GetType("System.String");
fNameColumn.ColumnName = "Fname";
fNameColumn.DefaultValue = "Fname";
namesTable.Columns.Add(fNameColumn);
DataColumn lNameColumn = new DataColumn();
lNameColumn.DataType = System.Type.GetType("System.String");
lNameColumn.ColumnName = "LName";
namesTable.Columns.Add(lNameColumn);
// Create an array for DataColumn objects.
DataColumn [] keys = new DataColumn [1];
keys[0] = idColumn;
namesTable.PrimaryKey = keys;
// Return the new DataTable.
return namesTable;
}

}

}
 

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