Sync combo box to current record on form

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I have a form with unbound controls on it, one of which is a combo who's
datasource is bound to a dataview. For example, say the combo's name is
'Fruit' and it's dataview has 2 columns in it, ID (int32) and Name (string)
and the following rows:
1 apple
2 orange
3 grape

When the record changes in the business class supporting this form and it's
'Fruit' property's value changes to 2, how do I get the combo to sync to the
row who's ID value is 2 (orange) and have 'orange' display in the text?

Thanks
 
Hi moondaddy,
You mean you want to notify the data-bound control when the underlying
values are changed. right?
But I'm not clear on the relation between your dataview and the business
object you mentioned.
If you bind the combobox with your business object, you should implement
the interface IBindingList, and fire the ListChanged event when you need
notify the data-bound control.
If you bind the combo box with DataView and the dataview is created from a
datatable, as I know, the combo box will be updated automatically if the
datatable changed, since DataView has already implemented the IDataBinding
Interface.
Here is an short sample which shows you how to implement an IBindingList
interface on you own.
and you may find more information in MSDN IBindingList
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcomponentmodelibindinglistclasstopic.asp
Does this solve your problem?
If you still have questions on this issue, please be free to let me know!
Thanks for using MSDN Newsgroup!
<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinForm_DataView
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(16, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Clear";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(152, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "Reload";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(240, 77);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


CustomersList list;
private void Form1_Load(object sender, System.EventArgs e)
{
list = new CustomersList();

list.LoadCustomers();
comboBox1.DataSource = list;
comboBox1.DisplayMember = "FirstName";
}

private void button1_Click(object sender, System.EventArgs e)
{
list.Clear();
}

private void button2_Click(object sender, System.EventArgs e)
{
list.LoadCustomers();
}

}
#region "CustomerList & Customer"
public class CustomersList : CollectionBase, IBindingList
{

private ListChangedEventArgs resetEvent = new
ListChangedEventArgs(ListChangedType.Reset, -1);
private ListChangedEventHandler onListChanged;

public void LoadCustomers()
{
IList l = (IList)this;
l.Add(ReadCustomer1());
l.Add(ReadCustomer2());
OnListChanged(resetEvent);
}

public Customer this[int index]
{
get
{
return (Customer)(List[index]);
}
set
{
List[index] = value;
}
}

public int Add (Customer value)
{
return List.Add(value);
}

public Customer AddNew()
{
return (Customer)((IBindingList)this).AddNew();
}

public void Remove (Customer value)
{
List.Remove(value);
}


protected virtual void OnListChanged(ListChangedEventArgs ev)
{
if (onListChanged != null)
{
onListChanged(this, ev);
}
}


protected override void OnClear()
{
foreach (Customer c in List)
{
c.Parent = null;
}
}

protected override void OnClearComplete()
{
OnListChanged(resetEvent);
}

protected override void OnInsertComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
index));
}

protected override void OnRemoveComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted,
index));
}

protected override void OnSetComplete(int index, object oldValue, object
newValue)
{
if (oldValue != newValue)
{

Customer oldcust = (Customer)oldValue;
Customer newcust = (Customer)newValue;

oldcust.Parent = null;
newcust.Parent = this;


OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
index));
}
}

// Called by Customer when it changes.
internal void CustomerChanged(Customer cust)
{

int index = List.IndexOf(cust);

OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged,
index));
}


// Implements IBindingList.
bool IBindingList.AllowEdit
{
get { return true ; }
}

bool IBindingList.AllowNew
{
get { return true ; }
}

bool IBindingList.AllowRemove
{
get { return true ; }
}

bool IBindingList.SupportsChangeNotification
{
get { return true ; }
}

bool IBindingList.SupportsSearching
{
get { return false ; }
}

bool IBindingList.SupportsSorting
{
get { return false ; }
}


// Events.
public event ListChangedEventHandler ListChanged
{
add
{
onListChanged += value;
}
remove
{
onListChanged -= value;
}
}

// Methods.
object IBindingList.AddNew()
{
Customer c = new Customer(this.Count.ToString());
List.Add(c);
return c;
}


// Unsupported properties.
bool IBindingList.IsSorted
{
get { throw new NotSupportedException(); }
}

ListSortDirection IBindingList.SortDirection
{
get { throw new NotSupportedException(); }
}


PropertyDescriptor IBindingList.SortProperty
{
get { throw new NotSupportedException(); }
}


// Unsupported Methods.
void IBindingList.AddIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.ApplySort(PropertyDescriptor property,
ListSortDirection direction)
{
throw new NotSupportedException();
}

int IBindingList.Find(PropertyDescriptor property, object key)
{
throw new NotSupportedException();
}

void IBindingList.RemoveIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.RemoveSort()
{
throw new NotSupportedException();
}

// Worker functions to populate the list with data.
private static Customer ReadCustomer1()
{
Customer cust = new Customer("536-45-1245");
cust.FirstName = "Jo";
cust.LastName = "Brown";
return cust;
}

private static Customer ReadCustomer2()
{
Customer cust = new Customer("246-12-5645");
cust.FirstName = "Robert";
cust.LastName = "Brown";
return cust;
}
}
public class Customer : IEditableObject
{

struct CustomerData
{
internal string id ;
internal string firstName ;
internal string lastName ;
}

private CustomersList parent;
private CustomerData custData;
private CustomerData backupData;
private bool inTxn = false;

// Implements IEditableObject
void IEditableObject.BeginEdit()
{
Console.WriteLine("Start BeginEdit");
if (!inTxn)
{
this.backupData = custData;
inTxn = true;
Console.WriteLine("BeginEdit - " + this.backupData.lastName);
}
Console.WriteLine("End BeginEdit");
}

void IEditableObject.CancelEdit()
{
Console.WriteLine("Start CancelEdit");
if (inTxn)
{
this.custData = backupData;
inTxn = false;
Console.WriteLine("CancelEdit - " + this.custData.lastName);
}
Console.WriteLine("End CancelEdit");
}

void IEditableObject.EndEdit()
{
Console.WriteLine("Start EndEdit" + this.custData.id +
this.custData.lastName);
if (inTxn)
{
backupData = new CustomerData();
inTxn = false;
Console.WriteLine("Done EndEdit - " + this.custData.id +
this.custData.lastName);
}
Console.WriteLine("End EndEdit");
}

public Customer(string ID) : base()
{
this.custData = new CustomerData();
this.custData.id = ID;
this.custData.firstName = "";
this.custData.lastName = "";
}

public string ID
{
get
{
return this.custData.id;
}
}

public string FirstName
{
get
{
return this.custData.firstName;
}
set
{
this.custData.firstName = value;
}
}

public string LastName
{
get
{
return this.custData.lastName;
}
set
{
this.custData.lastName = value;
}
}

internal CustomersList Parent
{
get
{
return parent;
}
set
{
parent = value ;
}
}

private void OnCustomerChanged()
{
if (!inTxn && Parent != null)
{
Parent.CustomerChanged(this);
}
}
}
#endregion
}

</code>




Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "moondaddy" <[email protected]>
| Subject: Sync combo box to current record on form
| Date: Wed, 15 Oct 2003 16:40:38 -0500
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: adsl-66-137-118-208.dsl.hstntx.swbell.net
66.137.118.208
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54528
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I have a form with unbound controls on it, one of which is a combo who's
| datasource is bound to a dataview. For example, say the combo's name is
| 'Fruit' and it's dataview has 2 columns in it, ID (int32) and Name
(string)
| and the following rows:
| 1 apple
| 2 orange
| 3 grape
|
| When the record changes in the business class supporting this form and
it's
| 'Fruit' property's value changes to 2, how do I get the combo to sync to
the
| row who's ID value is 2 (orange) and have 'orange' display in the text?
|
| Thanks
|
|
| --
| (e-mail address removed)
|
|
|
 
Thanks I'll look into this.

Ying-Shen Yu said:
Hi moondaddy,
You mean you want to notify the data-bound control when the underlying
values are changed. right?
But I'm not clear on the relation between your dataview and the business
object you mentioned.
If you bind the combobox with your business object, you should implement
the interface IBindingList, and fire the ListChanged event when you need
notify the data-bound control.
If you bind the combo box with DataView and the dataview is created from a
datatable, as I know, the combo box will be updated automatically if the
datatable changed, since DataView has already implemented the IDataBinding
Interface.
Here is an short sample which shows you how to implement an IBindingList
interface on you own.
and you may find more information in MSDN IBindingList
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcomponentmodelibindinglistclasstopic.asp
Does this solve your problem?
If you still have questions on this issue, please be free to let me know!
Thanks for using MSDN Newsgroup!
<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinForm_DataView
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(16, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Clear";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(152, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "Reload";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(240, 77);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


CustomersList list;
private void Form1_Load(object sender, System.EventArgs e)
{
list = new CustomersList();

list.LoadCustomers();
comboBox1.DataSource = list;
comboBox1.DisplayMember = "FirstName";
}

private void button1_Click(object sender, System.EventArgs e)
{
list.Clear();
}

private void button2_Click(object sender, System.EventArgs e)
{
list.LoadCustomers();
}

}
#region "CustomerList & Customer"
public class CustomersList : CollectionBase, IBindingList
{

private ListChangedEventArgs resetEvent = new
ListChangedEventArgs(ListChangedType.Reset, -1);
private ListChangedEventHandler onListChanged;

public void LoadCustomers()
{
IList l = (IList)this;
l.Add(ReadCustomer1());
l.Add(ReadCustomer2());
OnListChanged(resetEvent);
}

public Customer this[int index]
{
get
{
return (Customer)(List[index]);
}
set
{
List[index] = value;
}
}

public int Add (Customer value)
{
return List.Add(value);
}

public Customer AddNew()
{
return (Customer)((IBindingList)this).AddNew();
}

public void Remove (Customer value)
{
List.Remove(value);
}


protected virtual void OnListChanged(ListChangedEventArgs ev)
{
if (onListChanged != null)
{
onListChanged(this, ev);
}
}


protected override void OnClear()
{
foreach (Customer c in List)
{
c.Parent = null;
}
}

protected override void OnClearComplete()
{
OnListChanged(resetEvent);
}

protected override void OnInsertComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
index));
}

protected override void OnRemoveComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted,
index));
}

protected override void OnSetComplete(int index, object oldValue, object
newValue)
{
if (oldValue != newValue)
{

Customer oldcust = (Customer)oldValue;
Customer newcust = (Customer)newValue;

oldcust.Parent = null;
newcust.Parent = this;


OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
index));
}
}

// Called by Customer when it changes.
internal void CustomerChanged(Customer cust)
{

int index = List.IndexOf(cust);

OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged,
index));
}


// Implements IBindingList.
bool IBindingList.AllowEdit
{
get { return true ; }
}

bool IBindingList.AllowNew
{
get { return true ; }
}

bool IBindingList.AllowRemove
{
get { return true ; }
}

bool IBindingList.SupportsChangeNotification
{
get { return true ; }
}

bool IBindingList.SupportsSearching
{
get { return false ; }
}

bool IBindingList.SupportsSorting
{
get { return false ; }
}


// Events.
public event ListChangedEventHandler ListChanged
{
add
{
onListChanged += value;
}
remove
{
onListChanged -= value;
}
}

// Methods.
object IBindingList.AddNew()
{
Customer c = new Customer(this.Count.ToString());
List.Add(c);
return c;
}


// Unsupported properties.
bool IBindingList.IsSorted
{
get { throw new NotSupportedException(); }
}

ListSortDirection IBindingList.SortDirection
{
get { throw new NotSupportedException(); }
}


PropertyDescriptor IBindingList.SortProperty
{
get { throw new NotSupportedException(); }
}


// Unsupported Methods.
void IBindingList.AddIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.ApplySort(PropertyDescriptor property,
ListSortDirection direction)
{
throw new NotSupportedException();
}

int IBindingList.Find(PropertyDescriptor property, object key)
{
throw new NotSupportedException();
}

void IBindingList.RemoveIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.RemoveSort()
{
throw new NotSupportedException();
}

// Worker functions to populate the list with data.
private static Customer ReadCustomer1()
{
Customer cust = new Customer("536-45-1245");
cust.FirstName = "Jo";
cust.LastName = "Brown";
return cust;
}

private static Customer ReadCustomer2()
{
Customer cust = new Customer("246-12-5645");
cust.FirstName = "Robert";
cust.LastName = "Brown";
return cust;
}
}
public class Customer : IEditableObject
{

struct CustomerData
{
internal string id ;
internal string firstName ;
internal string lastName ;
}

private CustomersList parent;
private CustomerData custData;
private CustomerData backupData;
private bool inTxn = false;

// Implements IEditableObject
void IEditableObject.BeginEdit()
{
Console.WriteLine("Start BeginEdit");
if (!inTxn)
{
this.backupData = custData;
inTxn = true;
Console.WriteLine("BeginEdit - " + this.backupData.lastName);
}
Console.WriteLine("End BeginEdit");
}

void IEditableObject.CancelEdit()
{
Console.WriteLine("Start CancelEdit");
if (inTxn)
{
this.custData = backupData;
inTxn = false;
Console.WriteLine("CancelEdit - " + this.custData.lastName);
}
Console.WriteLine("End CancelEdit");
}

void IEditableObject.EndEdit()
{
Console.WriteLine("Start EndEdit" + this.custData.id +
this.custData.lastName);
if (inTxn)
{
backupData = new CustomerData();
inTxn = false;
Console.WriteLine("Done EndEdit - " + this.custData.id +
this.custData.lastName);
}
Console.WriteLine("End EndEdit");
}

public Customer(string ID) : base()
{
this.custData = new CustomerData();
this.custData.id = ID;
this.custData.firstName = "";
this.custData.lastName = "";
}

public string ID
{
get
{
return this.custData.id;
}
}

public string FirstName
{
get
{
return this.custData.firstName;
}
set
{
this.custData.firstName = value;
}
}

public string LastName
{
get
{
return this.custData.lastName;
}
set
{
this.custData.lastName = value;
}
}

internal CustomersList Parent
{
get
{
return parent;
}
set
{
parent = value ;
}
}

private void OnCustomerChanged()
{
if (!inTxn && Parent != null)
{
Parent.CustomerChanged(this);
}
}
}
#endregion
}

</code>




Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "moondaddy" <[email protected]>
| Subject: Sync combo box to current record on form
| Date: Wed, 15 Oct 2003 16:40:38 -0500
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: adsl-66-137-118-208.dsl.hstntx.swbell.net
66.137.118.208
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54528
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I have a form with unbound controls on it, one of which is a combo who's
| datasource is bound to a dataview. For example, say the combo's name is
| 'Fruit' and it's dataview has 2 columns in it, ID (int32) and Name
(string)
| and the following rows:
| 1 apple
| 2 orange
| 3 grape
|
| When the record changes in the business class supporting this form and
it's
| 'Fruit' property's value changes to 2, how do I get the combo to sync to
the
| row who's ID value is 2 (orange) and have 'orange' display in the text?
|
| Thanks
|
|
| --
| (e-mail address removed)
|
|
|
 
I'm sorry, I think I using the term "data-bound" incorrectly. All the data
entry contols on this form are unbound. This combo box I'm talking about
uses a dataset's datatable as the datasource which is what I meant to say
when I said databound. BIG apologies for that and I'm make an extra effort
not to be so sloppy in my postings.

OK, so this combo is unbound and has a datatable as the datasource. This
form represents an Order. when the user selects an order to navigate to,
this form is populated with data for the selected order. There's a separate
class which contains the state for this order along with any business logic
needed for the order. When the form is populated I call a method called
SetState which simply sets the value of each control in the form to the
corresponding property in the business class. For example, the combo named
"Or_EmpProcessedID" would be populated with the value from the
Or_EmpProcessedID property in the business class:


Public Property Or_EmpProcessedID() As Int32
Get
Try
If m_drProps.IsOr_EmpProcessedIDNull = True Then
Return Nothing
Else
Return m_drProps.Or_EmpProcessedID
End If
Catch ex As Exception
AddErrorToLog(ex)
End Try
End Get
Set(ByVal Value As Int32)
Try
If Value > 0 Then
m_drProps.Or_EmpProcessedID = Value
m_oRules.RuleBroken(BROKENRULE_Or_EmpProcessedID, False)
Else
m_drProps.Or_EmpProcessedID = 0
m_oRules.RuleBroken(BROKENRULE_Or_EmpProcessedID, True)
End If
If m_drProps.Or_EmpProcessedID = DataProps.Or_EmpProcessedID
Then
Else
m_flgDirty = True 'If new
value does not equal the origial value, then set m_flgDirty to TRUE
End If
Call Set_Class_Mode_And_State()
Catch ex As Exception
AddErrorToLog(ex)
End Try
End Set
End Property


So here's my big question: If the employee ID on this order is 23, how do I
set the combo to show the row who's ID value is 23 and show the correct
employee name in the text area? Currently I'm using the following code but
want to know if there's a better or more correct way to do it. If I have a
number of combo's with large datasources, this method seems inefficient.
Please let me know if there's a better way to do this.


Dim dvRow As DataRowView
Dim dv As New DataView(dsSprtData.Tables(1))
Dim objEnum As IEnumerator = dv.GetEnumerator
ID = objBOr.Or_EmpProcessedID
index = -1
Do While objEnum.MoveNext
dvRow = CType(objEnum.Current, DataRowView)
If ID = dvRow(0) Then
index = Or_EmpProcessedID.FindString(dvRow(1))
Exit Do
End If
Loop
Or_EmpProcessedID.SelectedIndex = index
If index = -1 Then Me.Or_EmpProcessedID.Text = ""


Thanks and sorry again for the miscommunication on my part!




Ying-Shen Yu said:
Hi moondaddy,
You mean you want to notify the data-bound control when the underlying
values are changed. right?
But I'm not clear on the relation between your dataview and the business
object you mentioned.
If you bind the combobox with your business object, you should implement
the interface IBindingList, and fire the ListChanged event when you need
notify the data-bound control.
If you bind the combo box with DataView and the dataview is created from a
datatable, as I know, the combo box will be updated automatically if the
datatable changed, since DataView has already implemented the IDataBinding
Interface.
Here is an short sample which shows you how to implement an IBindingList
interface on you own.
and you may find more information in MSDN IBindingList
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcomponentmodelibindinglistclasstopic.asp
Does this solve your problem?
If you still have questions on this issue, please be free to let me know!
Thanks for using MSDN Newsgroup!
<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinForm_DataView
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(16, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Clear";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(152, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "Reload";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(240, 77);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


CustomersList list;
private void Form1_Load(object sender, System.EventArgs e)
{
list = new CustomersList();

list.LoadCustomers();
comboBox1.DataSource = list;
comboBox1.DisplayMember = "FirstName";
}

private void button1_Click(object sender, System.EventArgs e)
{
list.Clear();
}

private void button2_Click(object sender, System.EventArgs e)
{
list.LoadCustomers();
}

}
#region "CustomerList & Customer"
public class CustomersList : CollectionBase, IBindingList
{

private ListChangedEventArgs resetEvent = new
ListChangedEventArgs(ListChangedType.Reset, -1);
private ListChangedEventHandler onListChanged;

public void LoadCustomers()
{
IList l = (IList)this;
l.Add(ReadCustomer1());
l.Add(ReadCustomer2());
OnListChanged(resetEvent);
}

public Customer this[int index]
{
get
{
return (Customer)(List[index]);
}
set
{
List[index] = value;
}
}

public int Add (Customer value)
{
return List.Add(value);
}

public Customer AddNew()
{
return (Customer)((IBindingList)this).AddNew();
}

public void Remove (Customer value)
{
List.Remove(value);
}


protected virtual void OnListChanged(ListChangedEventArgs ev)
{
if (onListChanged != null)
{
onListChanged(this, ev);
}
}


protected override void OnClear()
{
foreach (Customer c in List)
{
c.Parent = null;
}
}

protected override void OnClearComplete()
{
OnListChanged(resetEvent);
}

protected override void OnInsertComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
index));
}

protected override void OnRemoveComplete(int index, object value)
{
Customer c = (Customer)value;
c.Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted,
index));
}

protected override void OnSetComplete(int index, object oldValue, object
newValue)
{
if (oldValue != newValue)
{

Customer oldcust = (Customer)oldValue;
Customer newcust = (Customer)newValue;

oldcust.Parent = null;
newcust.Parent = this;


OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
index));
}
}

// Called by Customer when it changes.
internal void CustomerChanged(Customer cust)
{

int index = List.IndexOf(cust);

OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged,
index));
}


// Implements IBindingList.
bool IBindingList.AllowEdit
{
get { return true ; }
}

bool IBindingList.AllowNew
{
get { return true ; }
}

bool IBindingList.AllowRemove
{
get { return true ; }
}

bool IBindingList.SupportsChangeNotification
{
get { return true ; }
}

bool IBindingList.SupportsSearching
{
get { return false ; }
}

bool IBindingList.SupportsSorting
{
get { return false ; }
}


// Events.
public event ListChangedEventHandler ListChanged
{
add
{
onListChanged += value;
}
remove
{
onListChanged -= value;
}
}

// Methods.
object IBindingList.AddNew()
{
Customer c = new Customer(this.Count.ToString());
List.Add(c);
return c;
}


// Unsupported properties.
bool IBindingList.IsSorted
{
get { throw new NotSupportedException(); }
}

ListSortDirection IBindingList.SortDirection
{
get { throw new NotSupportedException(); }
}


PropertyDescriptor IBindingList.SortProperty
{
get { throw new NotSupportedException(); }
}


// Unsupported Methods.
void IBindingList.AddIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.ApplySort(PropertyDescriptor property,
ListSortDirection direction)
{
throw new NotSupportedException();
}

int IBindingList.Find(PropertyDescriptor property, object key)
{
throw new NotSupportedException();
}

void IBindingList.RemoveIndex(PropertyDescriptor property)
{
throw new NotSupportedException();
}

void IBindingList.RemoveSort()
{
throw new NotSupportedException();
}

// Worker functions to populate the list with data.
private static Customer ReadCustomer1()
{
Customer cust = new Customer("536-45-1245");
cust.FirstName = "Jo";
cust.LastName = "Brown";
return cust;
}

private static Customer ReadCustomer2()
{
Customer cust = new Customer("246-12-5645");
cust.FirstName = "Robert";
cust.LastName = "Brown";
return cust;
}
}
public class Customer : IEditableObject
{

struct CustomerData
{
internal string id ;
internal string firstName ;
internal string lastName ;
}

private CustomersList parent;
private CustomerData custData;
private CustomerData backupData;
private bool inTxn = false;

// Implements IEditableObject
void IEditableObject.BeginEdit()
{
Console.WriteLine("Start BeginEdit");
if (!inTxn)
{
this.backupData = custData;
inTxn = true;
Console.WriteLine("BeginEdit - " + this.backupData.lastName);
}
Console.WriteLine("End BeginEdit");
}

void IEditableObject.CancelEdit()
{
Console.WriteLine("Start CancelEdit");
if (inTxn)
{
this.custData = backupData;
inTxn = false;
Console.WriteLine("CancelEdit - " + this.custData.lastName);
}
Console.WriteLine("End CancelEdit");
}

void IEditableObject.EndEdit()
{
Console.WriteLine("Start EndEdit" + this.custData.id +
this.custData.lastName);
if (inTxn)
{
backupData = new CustomerData();
inTxn = false;
Console.WriteLine("Done EndEdit - " + this.custData.id +
this.custData.lastName);
}
Console.WriteLine("End EndEdit");
}

public Customer(string ID) : base()
{
this.custData = new CustomerData();
this.custData.id = ID;
this.custData.firstName = "";
this.custData.lastName = "";
}

public string ID
{
get
{
return this.custData.id;
}
}

public string FirstName
{
get
{
return this.custData.firstName;
}
set
{
this.custData.firstName = value;
}
}

public string LastName
{
get
{
return this.custData.lastName;
}
set
{
this.custData.lastName = value;
}
}

internal CustomersList Parent
{
get
{
return parent;
}
set
{
parent = value ;
}
}

private void OnCustomerChanged()
{
if (!inTxn && Parent != null)
{
Parent.CustomerChanged(this);
}
}
}
#endregion
}

</code>




Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "moondaddy" <[email protected]>
| Subject: Sync combo box to current record on form
| Date: Wed, 15 Oct 2003 16:40:38 -0500
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: adsl-66-137-118-208.dsl.hstntx.swbell.net
66.137.118.208
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54528
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I have a form with unbound controls on it, one of which is a combo who's
| datasource is bound to a dataview. For example, say the combo's name is
| 'Fruit' and it's dataview has 2 columns in it, ID (int32) and Name
(string)
| and the following rows:
| 1 apple
| 2 orange
| 3 grape
|
| When the record changes in the business class supporting this form and
it's
| 'Fruit' property's value changes to 2, how do I get the combo to sync to
the
| row who's ID value is 2 (orange) and have 'orange' display in the text?
|
| Thanks
|
|
| --
| (e-mail address removed)
|
|
|
 
Hi moondaddy,
I feel sorry for not fully understanding your problem.
Here is my understanding now, please let me know if I'm wrong.
You want to change the text in combobox in the Set_Class_Mode_And_State
method. You are now using a loop to find an item whose ID is same as the
value set to your business object.

You asked if there is any better way to do this.
With limited information, I can only give some advice,
I think the optimization depends on the number of ID.
If the number of ID is in sequence( such as 1,2,3,4...), you may try using
the value minus 1 directly as the SelectedIndex.
If the number of ID is not in sequence but has some pattern, you may try
mapping it to the Item index.
If the number of ID is of random, maybe you can save the IDs in a seperate
int array and search the index using Array.IndexOf method.
Does it answer your question?
Please let me know if you still have questions on this issue.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "moondaddy" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Sync combo box to current record on form
| Date: Mon, 20 Oct 2003 15:11:05 -0500
| Lines: 640
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: adsl-66-137-116-21.dsl.hstntx.swbell.net 66.137.116.21
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54854
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I'm sorry, I think I using the term "data-bound" incorrectly. All the
data
| entry contols on this form are unbound. This combo box I'm talking about
| uses a dataset's datatable as the datasource which is what I meant to say
| when I said databound. BIG apologies for that and I'm make an extra
effort
| not to be so sloppy in my postings.
|
| OK, so this combo is unbound and has a datatable as the datasource. This
| form represents an Order. when the user selects an order to navigate to,
| this form is populated with data for the selected order. There's a
separate
| class which contains the state for this order along with any business
logic
| needed for the order. When the form is populated I call a method called
| SetState which simply sets the value of each control in the form to the
| corresponding property in the business class. For example, the combo
named
| "Or_EmpProcessedID" would be populated with the value from the
| Or_EmpProcessedID property in the business class:
|
|
| Public Property Or_EmpProcessedID() As Int32
| Get
| Try
| If m_drProps.IsOr_EmpProcessedIDNull = True Then
| Return Nothing
| Else
| Return m_drProps.Or_EmpProcessedID
| End If
| Catch ex As Exception
| AddErrorToLog(ex)
| End Try
| End Get
| Set(ByVal Value As Int32)
| Try
| If Value > 0 Then
| m_drProps.Or_EmpProcessedID = Value
| m_oRules.RuleBroken(BROKENRULE_Or_EmpProcessedID,
False)
| Else
| m_drProps.Or_EmpProcessedID = 0
| m_oRules.RuleBroken(BROKENRULE_Or_EmpProcessedID,
True)
| End If
| If m_drProps.Or_EmpProcessedID =
DataProps.Or_EmpProcessedID
| Then
| Else
| m_flgDirty = True 'If new
| value does not equal the origial value, then set m_flgDirty to TRUE
| End If
| Call Set_Class_Mode_And_State()
| Catch ex As Exception
| AddErrorToLog(ex)
| End Try
| End Set
| End Property
|
|
| So here's my big question: If the employee ID on this order is 23, how
do I
| set the combo to show the row who's ID value is 23 and show the correct
| employee name in the text area? Currently I'm using the following code
but
| want to know if there's a better or more correct way to do it. If I have
a
| number of combo's with large datasources, this method seems inefficient.
| Please let me know if there's a better way to do this.
|
|
| Dim dvRow As DataRowView
| Dim dv As New DataView(dsSprtData.Tables(1))
| Dim objEnum As IEnumerator = dv.GetEnumerator
| ID = objBOr.Or_EmpProcessedID
| index = -1
| Do While objEnum.MoveNext
| dvRow = CType(objEnum.Current, DataRowView)
| If ID = dvRow(0) Then
| index = Or_EmpProcessedID.FindString(dvRow(1))
| Exit Do
| End If
| Loop
| Or_EmpProcessedID.SelectedIndex = index
| If index = -1 Then Me.Or_EmpProcessedID.Text = ""
|
|
| Thanks and sorry again for the miscommunication on my part!
|
|
|
|
| | > Hi moondaddy,
| > You mean you want to notify the data-bound control when the underlying
| > values are changed. right?
| > But I'm not clear on the relation between your dataview and the business
| > object you mentioned.
| > If you bind the combobox with your business object, you should implement
| > the interface IBindingList, and fire the ListChanged event when you need
| > notify the data-bound control.
| > If you bind the combo box with DataView and the dataview is created
from a
| > datatable, as I know, the combo box will be updated automatically if the
| > datatable changed, since DataView has already implemented the
IDataBinding
| > Interface.
| > Here is an short sample which shows you how to implement an IBindingList
| > interface on you own.
| > and you may find more information in MSDN IBindingList
| >
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
| > frlrfsystemcomponentmodelibindinglistclasstopic.asp
| > Does this solve your problem?
| > If you still have questions on this issue, please be free to let me
know!
| > Thanks for using MSDN Newsgroup!
| > <code>
| > using System;
| > using System.Drawing;
| > using System.Collections;
| > using System.ComponentModel;
| > using System.Windows.Forms;
| > using System.Data;
| >
| > namespace WinForm_DataView
| > {
| > /// <summary>
| > /// Summary description for Form1.
| > /// </summary>
| > public class Form1 : System.Windows.Forms.Form
| > {
| > private System.Windows.Forms.ComboBox comboBox1;
| > private System.Windows.Forms.Button button1;
| > private System.Windows.Forms.Button button2;
| > /// <summary>
| > /// Required designer variable.
| > /// </summary>
| > private System.ComponentModel.Container components = null;
| >
| > public Form1()
| > {
| > //
| > // Required for Windows Form Designer support
| > //
| > InitializeComponent();
| >
| > //
| > // TODO: Add any constructor code after InitializeComponent call
| > //
| > }
| >
| > /// <summary>
| > /// Clean up any resources being used.
| > /// </summary>
| > protected override void Dispose( bool disposing )
| > {
| > if( disposing )
| > {
| > if (components != null)
| > {
| > components.Dispose();
| > }
| > }
| > 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.button1 = new System.Windows.Forms.Button();
| > this.button2 = new System.Windows.Forms.Button();
| > this.SuspendLayout();
| > //
| > // comboBox1
| > //
| > this.comboBox1.Location = new System.Drawing.Point(16, 16);
| > this.comboBox1.Name = "comboBox1";
| > this.comboBox1.Size = new System.Drawing.Size(121, 21);
| > this.comboBox1.TabIndex = 0;
| > this.comboBox1.Text = "comboBox1";
| > //
| > // button1
| > //
| > this.button1.Location = new System.Drawing.Point(152, 48);
| > this.button1.Name = "button1";
| > this.button1.TabIndex = 1;
| > this.button1.Text = "Clear";
| > this.button1.Click += new System.EventHandler(this.button1_Click);
| > //
| > // button2
| > //
| > this.button2.Location = new System.Drawing.Point(152, 16);
| > this.button2.Name = "button2";
| > this.button2.TabIndex = 4;
| > this.button2.Text = "Reload";
| > this.button2.Click += new System.EventHandler(this.button2_Click);
| > //
| > // Form1
| > //
| > this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
| > this.CausesValidation = false;
| > this.ClientSize = new System.Drawing.Size(240, 77);
| > this.Controls.Add(this.button2);
| > this.Controls.Add(this.button1);
| > this.Controls.Add(this.comboBox1);
| > this.Name = "Form1";
| > this.Text = "Form1";
| > this.Load += new System.EventHandler(this.Form1_Load);
| > this.ResumeLayout(false);
| >
| > }
| > #endregion
| >
| > /// <summary>
| > /// The main entry point for the application.
| > /// </summary>
| > [STAThread]
| > static void Main()
| > {
| > Application.Run(new Form1());
| > }
| >
| >
| > CustomersList list;
| > private void Form1_Load(object sender, System.EventArgs e)
| > {
| > list = new CustomersList();
| >
| > list.LoadCustomers();
| > comboBox1.DataSource = list;
| > comboBox1.DisplayMember = "FirstName";
| > }
| >
| > private void button1_Click(object sender, System.EventArgs e)
| > {
| > list.Clear();
| > }
| >
| > private void button2_Click(object sender, System.EventArgs e)
| > {
| > list.LoadCustomers();
| > }
| >
| > }
| > #region "CustomerList & Customer"
| > public class CustomersList : CollectionBase, IBindingList
| > {
| >
| > private ListChangedEventArgs resetEvent = new
| > ListChangedEventArgs(ListChangedType.Reset, -1);
| > private ListChangedEventHandler onListChanged;
| >
| > public void LoadCustomers()
| > {
| > IList l = (IList)this;
| > l.Add(ReadCustomer1());
| > l.Add(ReadCustomer2());
| > OnListChanged(resetEvent);
| > }
| >
| > public Customer this[int index]
| > {
| > get
| > {
| > return (Customer)(List[index]);
| > }
| > set
| > {
| > List[index] = value;
| > }
| > }
| >
| > public int Add (Customer value)
| > {
| > return List.Add(value);
| > }
| >
| > public Customer AddNew()
| > {
| > return (Customer)((IBindingList)this).AddNew();
| > }
| >
| > public void Remove (Customer value)
| > {
| > List.Remove(value);
| > }
| >
| >
| > protected virtual void OnListChanged(ListChangedEventArgs ev)
| > {
| > if (onListChanged != null)
| > {
| > onListChanged(this, ev);
| > }
| > }
| >
| >
| > protected override void OnClear()
| > {
| > foreach (Customer c in List)
| > {
| > c.Parent = null;
| > }
| > }
| >
| > protected override void OnClearComplete()
| > {
| > OnListChanged(resetEvent);
| > }
| >
| > protected override void OnInsertComplete(int index, object value)
| > {
| > Customer c = (Customer)value;
| > c.Parent = this;
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
| > index));
| > }
| >
| > protected override void OnRemoveComplete(int index, object value)
| > {
| > Customer c = (Customer)value;
| > c.Parent = this;
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted,
| > index));
| > }
| >
| > protected override void OnSetComplete(int index, object oldValue, object
| > newValue)
| > {
| > if (oldValue != newValue)
| > {
| >
| > Customer oldcust = (Customer)oldValue;
| > Customer newcust = (Customer)newValue;
| >
| > oldcust.Parent = null;
| > newcust.Parent = this;
| >
| >
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
| > index));
| > }
| > }
| >
| > // Called by Customer when it changes.
| > internal void CustomerChanged(Customer cust)
| > {
| >
| > int index = List.IndexOf(cust);
| >
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged,
| > index));
| > }
| >
| >
| > // Implements IBindingList.
| > bool IBindingList.AllowEdit
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.AllowNew
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.AllowRemove
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.SupportsChangeNotification
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.SupportsSearching
| > {
| > get { return false ; }
| > }
| >
| > bool IBindingList.SupportsSorting
| > {
| > get { return false ; }
| > }
| >
| >
| > // Events.
| > public event ListChangedEventHandler ListChanged
| > {
| > add
| > {
| > onListChanged += value;
| > }
| > remove
| > {
| > onListChanged -= value;
| > }
| > }
| >
| > // Methods.
| > object IBindingList.AddNew()
| > {
| > Customer c = new Customer(this.Count.ToString());
| > List.Add(c);
| > return c;
| > }
| >
| >
| > // Unsupported properties.
| > bool IBindingList.IsSorted
| > {
| > get { throw new NotSupportedException(); }
| > }
| >
| > ListSortDirection IBindingList.SortDirection
| > {
| > get { throw new NotSupportedException(); }
| > }
| >
| >
| > PropertyDescriptor IBindingList.SortProperty
| > {
| > get { throw new NotSupportedException(); }
| > }
| >
| >
| > // Unsupported Methods.
| > void IBindingList.AddIndex(PropertyDescriptor property)
| > {
| > throw new NotSupportedException();
| > }
| >
| > void IBindingList.ApplySort(PropertyDescriptor property,
| > ListSortDirection direction)
| > {
| > throw new NotSupportedException();
| > }
| >
| > int IBindingList.Find(PropertyDescriptor property, object key)
| > {
| > throw new NotSupportedException();
| > }
| >
| > void IBindingList.RemoveIndex(PropertyDescriptor property)
| > {
| > throw new NotSupportedException();
| > }
| >
| > void IBindingList.RemoveSort()
| > {
| > throw new NotSupportedException();
| > }
| >
| > // Worker functions to populate the list with data.
| > private static Customer ReadCustomer1()
| > {
| > Customer cust = new Customer("536-45-1245");
| > cust.FirstName = "Jo";
| > cust.LastName = "Brown";
| > return cust;
| > }
| >
| > private static Customer ReadCustomer2()
| > {
| > Customer cust = new Customer("246-12-5645");
| > cust.FirstName = "Robert";
| > cust.LastName = "Brown";
| > return cust;
| > }
| > }
| > public class Customer : IEditableObject
| > {
| >
| > struct CustomerData
| > {
| > internal string id ;
| > internal string firstName ;
| > internal string lastName ;
| > }
| >
| > private CustomersList parent;
| > private CustomerData custData;
| > private CustomerData backupData;
| > private bool inTxn = false;
| >
| > // Implements IEditableObject
| > void IEditableObject.BeginEdit()
| > {
| > Console.WriteLine("Start BeginEdit");
| > if (!inTxn)
| > {
| > this.backupData = custData;
| > inTxn = true;
| > Console.WriteLine("BeginEdit - " + this.backupData.lastName);
| > }
| > Console.WriteLine("End BeginEdit");
| > }
| >
| > void IEditableObject.CancelEdit()
| > {
| > Console.WriteLine("Start CancelEdit");
| > if (inTxn)
| > {
| > this.custData = backupData;
| > inTxn = false;
| > Console.WriteLine("CancelEdit - " + this.custData.lastName);
| > }
| > Console.WriteLine("End CancelEdit");
| > }
| >
| > void IEditableObject.EndEdit()
| > {
| > Console.WriteLine("Start EndEdit" + this.custData.id +
| > this.custData.lastName);
| > if (inTxn)
| > {
| > backupData = new CustomerData();
| > inTxn = false;
| > Console.WriteLine("Done EndEdit - " + this.custData.id +
| > this.custData.lastName);
| > }
| > Console.WriteLine("End EndEdit");
| > }
| >
| > public Customer(string ID) : base()
| > {
| > this.custData = new CustomerData();
| > this.custData.id = ID;
| > this.custData.firstName = "";
| > this.custData.lastName = "";
| > }
| >
| > public string ID
| > {
| > get
| > {
| > return this.custData.id;
| > }
| > }
| >
| > public string FirstName
| > {
| > get
| > {
| > return this.custData.firstName;
| > }
| > set
| > {
| > this.custData.firstName = value;
| > }
| > }
| >
| > public string LastName
| > {
| > get
| > {
| > return this.custData.lastName;
| > }
| > set
| > {
| > this.custData.lastName = value;
| > }
| > }
| >
| > internal CustomersList Parent
| > {
| > get
| > {
| > return parent;
| > }
| > set
| > {
| > parent = value ;
| > }
| > }
| >
| > private void OnCustomerChanged()
| > {
| > if (!inTxn && Parent != null)
| > {
| > Parent.CustomerChanged(this);
| > }
| > }
| > }
| > #endregion
| > }
| >
| > </code>
| >
| >
| >
| >
| > Best regards,
| >
| > Ying-Shen Yu [MSFT]
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| >
| > This posting is provided "AS IS" with no warranties and confers no
rights.
| > You should not reply this mail directly, "Online" should be removed
before
| > sending, Thanks!
| >
| > --------------------
| > | From: "moondaddy" <[email protected]>
| > | Subject: Sync combo box to current record on form
| > | Date: Wed, 15 Oct 2003 16:40:38 -0500
| > | Lines: 19
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: adsl-66-137-118-208.dsl.hstntx.swbell.net
| > 66.137.118.208
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:54528
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | I have a form with unbound controls on it, one of which is a combo
who's
| > | datasource is bound to a dataview. For example, say the combo's name
is
| > | 'Fruit' and it's dataview has 2 columns in it, ID (int32) and Name
| > (string)
| > | and the following rows:
| > | 1 apple
| > | 2 orange
| > | 3 grape
| > |
| > | When the record changes in the business class supporting this form and
| > it's
| > | 'Fruit' property's value changes to 2, how do I get the combo to sync
to
| > the
| > | row who's ID value is 2 (orange) and have 'orange' display in the
text?
| > |
| > | Thanks
| > |
| > |
| > | --
| > | (e-mail address removed)
| > |
| > |
| > |
| >
|
|
|
 
Yes you do understand my question now and I think I understand the answer.
In other words, there is no "one-best" way to find an item in list based on
the ID value which isn't displayed in the text area (I think the ID column
is what would normally be the valuemember as apposed to the displaymember).
Since in most (or all) of my cases the ID column is based on a table's (PK
and identity, increment 1) but the dataset (or datatable) is always
presorted on a text column such as employee name or something, then the ID
values will be random. I was hoping for a silver bullet like a find ID
method or something like you might find in 3rd party tools.

Thanks for clearing this up for me.




Ying-Shen Yu said:
Hi moondaddy,
I feel sorry for not fully understanding your problem.
Here is my understanding now, please let me know if I'm wrong.
You want to change the text in combobox in the Set_Class_Mode_And_State
method. You are now using a loop to find an item whose ID is same as the
value set to your business object.

You asked if there is any better way to do this.
With limited information, I can only give some advice,
I think the optimization depends on the number of ID.
If the number of ID is in sequence( such as 1,2,3,4...), you may try using
the value minus 1 directly as the SelectedIndex.
If the number of ID is not in sequence but has some pattern, you may try
mapping it to the Item index.
If the number of ID is of random, maybe you can save the IDs in a seperate
int array and search the index using Array.IndexOf method.
Does it answer your question?
Please let me know if you still have questions on this issue.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "moondaddy" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Sync combo box to current record on form
| Date: Mon, 20 Oct 2003 15:11:05 -0500
| Lines: 640
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: adsl-66-137-116-21.dsl.hstntx.swbell.net 66.137.116.21
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54854
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I'm sorry, I think I using the term "data-bound" incorrectly. All the
data
| entry contols on this form are unbound. This combo box I'm talking about
| uses a dataset's datatable as the datasource which is what I meant to say
| when I said databound. BIG apologies for that and I'm make an extra
effort
| not to be so sloppy in my postings.
|
| OK, so this combo is unbound and has a datatable as the datasource. This
| form represents an Order. when the user selects an order to navigate to,
| this form is populated with data for the selected order. There's a
separate
| class which contains the state for this order along with any business
logic
| needed for the order. When the form is populated I call a method called
| SetState which simply sets the value of each control in the form to the
| corresponding property in the business class. For example, the combo
named
| "Or_EmpProcessedID" would be populated with the value from the
| Or_EmpProcessedID property in the business class:
|
|
| Public Property Or_EmpProcessedID() As Int32
| Get
| Try
| If m_drProps.IsOr_EmpProcessedIDNull = True Then
| Return Nothing
| Else
| Return m_drProps.Or_EmpProcessedID
| End If
| Catch ex As Exception
| AddErrorToLog(ex)
| End Try
| End Get
| Set(ByVal Value As Int32)
| Try
| If Value > 0 Then
| m_drProps.Or_EmpProcessedID = Value
| m_oRules.RuleBroken(BROKENRULE_Or_EmpProcessedID,
False)
| Else
| m_drProps.Or_EmpProcessedID = 0
| m_oRules.RuleBroken(BROKENRULE_Or_EmpProcessedID,
True)
| End If
| If m_drProps.Or_EmpProcessedID =
DataProps.Or_EmpProcessedID
| Then
| Else
| m_flgDirty = True 'If new
| value does not equal the origial value, then set m_flgDirty to TRUE
| End If
| Call Set_Class_Mode_And_State()
| Catch ex As Exception
| AddErrorToLog(ex)
| End Try
| End Set
| End Property
|
|
| So here's my big question: If the employee ID on this order is 23, how
do I
| set the combo to show the row who's ID value is 23 and show the correct
| employee name in the text area? Currently I'm using the following code
but
| want to know if there's a better or more correct way to do it. If I have
a
| number of combo's with large datasources, this method seems inefficient.
| Please let me know if there's a better way to do this.
|
|
| Dim dvRow As DataRowView
| Dim dv As New DataView(dsSprtData.Tables(1))
| Dim objEnum As IEnumerator = dv.GetEnumerator
| ID = objBOr.Or_EmpProcessedID
| index = -1
| Do While objEnum.MoveNext
| dvRow = CType(objEnum.Current, DataRowView)
| If ID = dvRow(0) Then
| index = Or_EmpProcessedID.FindString(dvRow(1))
| Exit Do
| End If
| Loop
| Or_EmpProcessedID.SelectedIndex = index
| If index = -1 Then Me.Or_EmpProcessedID.Text = ""
|
|
| Thanks and sorry again for the miscommunication on my part!
|
|
|
|
| | > Hi moondaddy,
| > You mean you want to notify the data-bound control when the underlying
| > values are changed. right?
| > But I'm not clear on the relation between your dataview and the business
| > object you mentioned.
| > If you bind the combobox with your business object, you should implement
| > the interface IBindingList, and fire the ListChanged event when you need
| > notify the data-bound control.
| > If you bind the combo box with DataView and the dataview is created
from a
| > datatable, as I know, the combo box will be updated automatically if the
| > datatable changed, since DataView has already implemented the
IDataBinding
| > Interface.
| > Here is an short sample which shows you how to implement an IBindingList
| > interface on you own.
| > and you may find more information in MSDN IBindingList
| >
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
| > frlrfsystemcomponentmodelibindinglistclasstopic.asp
| > Does this solve your problem?
| > If you still have questions on this issue, please be free to let me
know!
| > Thanks for using MSDN Newsgroup!
| > <code>
| > using System;
| > using System.Drawing;
| > using System.Collections;
| > using System.ComponentModel;
| > using System.Windows.Forms;
| > using System.Data;
| >
| > namespace WinForm_DataView
| > {
| > /// <summary>
| > /// Summary description for Form1.
| > /// </summary>
| > public class Form1 : System.Windows.Forms.Form
| > {
| > private System.Windows.Forms.ComboBox comboBox1;
| > private System.Windows.Forms.Button button1;
| > private System.Windows.Forms.Button button2;
| > /// <summary>
| > /// Required designer variable.
| > /// </summary>
| > private System.ComponentModel.Container components = null;
| >
| > public Form1()
| > {
| > //
| > // Required for Windows Form Designer support
| > //
| > InitializeComponent();
| >
| > //
| > // TODO: Add any constructor code after InitializeComponent call
| > //
| > }
| >
| > /// <summary>
| > /// Clean up any resources being used.
| > /// </summary>
| > protected override void Dispose( bool disposing )
| > {
| > if( disposing )
| > {
| > if (components != null)
| > {
| > components.Dispose();
| > }
| > }
| > 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.button1 = new System.Windows.Forms.Button();
| > this.button2 = new System.Windows.Forms.Button();
| > this.SuspendLayout();
| > //
| > // comboBox1
| > //
| > this.comboBox1.Location = new System.Drawing.Point(16, 16);
| > this.comboBox1.Name = "comboBox1";
| > this.comboBox1.Size = new System.Drawing.Size(121, 21);
| > this.comboBox1.TabIndex = 0;
| > this.comboBox1.Text = "comboBox1";
| > //
| > // button1
| > //
| > this.button1.Location = new System.Drawing.Point(152, 48);
| > this.button1.Name = "button1";
| > this.button1.TabIndex = 1;
| > this.button1.Text = "Clear";
| > this.button1.Click += new System.EventHandler(this.button1_Click);
| > //
| > // button2
| > //
| > this.button2.Location = new System.Drawing.Point(152, 16);
| > this.button2.Name = "button2";
| > this.button2.TabIndex = 4;
| > this.button2.Text = "Reload";
| > this.button2.Click += new System.EventHandler(this.button2_Click);
| > //
| > // Form1
| > //
| > this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
| > this.CausesValidation = false;
| > this.ClientSize = new System.Drawing.Size(240, 77);
| > this.Controls.Add(this.button2);
| > this.Controls.Add(this.button1);
| > this.Controls.Add(this.comboBox1);
| > this.Name = "Form1";
| > this.Text = "Form1";
| > this.Load += new System.EventHandler(this.Form1_Load);
| > this.ResumeLayout(false);
| >
| > }
| > #endregion
| >
| > /// <summary>
| > /// The main entry point for the application.
| > /// </summary>
| > [STAThread]
| > static void Main()
| > {
| > Application.Run(new Form1());
| > }
| >
| >
| > CustomersList list;
| > private void Form1_Load(object sender, System.EventArgs e)
| > {
| > list = new CustomersList();
| >
| > list.LoadCustomers();
| > comboBox1.DataSource = list;
| > comboBox1.DisplayMember = "FirstName";
| > }
| >
| > private void button1_Click(object sender, System.EventArgs e)
| > {
| > list.Clear();
| > }
| >
| > private void button2_Click(object sender, System.EventArgs e)
| > {
| > list.LoadCustomers();
| > }
| >
| > }
| > #region "CustomerList & Customer"
| > public class CustomersList : CollectionBase, IBindingList
| > {
| >
| > private ListChangedEventArgs resetEvent = new
| > ListChangedEventArgs(ListChangedType.Reset, -1);
| > private ListChangedEventHandler onListChanged;
| >
| > public void LoadCustomers()
| > {
| > IList l = (IList)this;
| > l.Add(ReadCustomer1());
| > l.Add(ReadCustomer2());
| > OnListChanged(resetEvent);
| > }
| >
| > public Customer this[int index]
| > {
| > get
| > {
| > return (Customer)(List[index]);
| > }
| > set
| > {
| > List[index] = value;
| > }
| > }
| >
| > public int Add (Customer value)
| > {
| > return List.Add(value);
| > }
| >
| > public Customer AddNew()
| > {
| > return (Customer)((IBindingList)this).AddNew();
| > }
| >
| > public void Remove (Customer value)
| > {
| > List.Remove(value);
| > }
| >
| >
| > protected virtual void OnListChanged(ListChangedEventArgs ev)
| > {
| > if (onListChanged != null)
| > {
| > onListChanged(this, ev);
| > }
| > }
| >
| >
| > protected override void OnClear()
| > {
| > foreach (Customer c in List)
| > {
| > c.Parent = null;
| > }
| > }
| >
| > protected override void OnClearComplete()
| > {
| > OnListChanged(resetEvent);
| > }
| >
| > protected override void OnInsertComplete(int index, object value)
| > {
| > Customer c = (Customer)value;
| > c.Parent = this;
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
| > index));
| > }
| >
| > protected override void OnRemoveComplete(int index, object value)
| > {
| > Customer c = (Customer)value;
| > c.Parent = this;
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted,
| > index));
| > }
| >
| > protected override void OnSetComplete(int index, object oldValue, object
| > newValue)
| > {
| > if (oldValue != newValue)
| > {
| >
| > Customer oldcust = (Customer)oldValue;
| > Customer newcust = (Customer)newValue;
| >
| > oldcust.Parent = null;
| > newcust.Parent = this;
| >
| >
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
| > index));
| > }
| > }
| >
| > // Called by Customer when it changes.
| > internal void CustomerChanged(Customer cust)
| > {
| >
| > int index = List.IndexOf(cust);
| >
| > OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged,
| > index));
| > }
| >
| >
| > // Implements IBindingList.
| > bool IBindingList.AllowEdit
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.AllowNew
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.AllowRemove
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.SupportsChangeNotification
| > {
| > get { return true ; }
| > }
| >
| > bool IBindingList.SupportsSearching
| > {
| > get { return false ; }
| > }
| >
| > bool IBindingList.SupportsSorting
| > {
| > get { return false ; }
| > }
| >
| >
| > // Events.
| > public event ListChangedEventHandler ListChanged
| > {
| > add
| > {
| > onListChanged += value;
| > }
| > remove
| > {
| > onListChanged -= value;
| > }
| > }
| >
| > // Methods.
| > object IBindingList.AddNew()
| > {
| > Customer c = new Customer(this.Count.ToString());
| > List.Add(c);
| > return c;
| > }
| >
| >
| > // Unsupported properties.
| > bool IBindingList.IsSorted
| > {
| > get { throw new NotSupportedException(); }
| > }
| >
| > ListSortDirection IBindingList.SortDirection
| > {
| > get { throw new NotSupportedException(); }
| > }
| >
| >
| > PropertyDescriptor IBindingList.SortProperty
| > {
| > get { throw new NotSupportedException(); }
| > }
| >
| >
| > // Unsupported Methods.
| > void IBindingList.AddIndex(PropertyDescriptor property)
| > {
| > throw new NotSupportedException();
| > }
| >
| > void IBindingList.ApplySort(PropertyDescriptor property,
| > ListSortDirection direction)
| > {
| > throw new NotSupportedException();
| > }
| >
| > int IBindingList.Find(PropertyDescriptor property, object key)
| > {
| > throw new NotSupportedException();
| > }
| >
| > void IBindingList.RemoveIndex(PropertyDescriptor property)
| > {
| > throw new NotSupportedException();
| > }
| >
| > void IBindingList.RemoveSort()
| > {
| > throw new NotSupportedException();
| > }
| >
| > // Worker functions to populate the list with data.
| > private static Customer ReadCustomer1()
| > {
| > Customer cust = new Customer("536-45-1245");
| > cust.FirstName = "Jo";
| > cust.LastName = "Brown";
| > return cust;
| > }
| >
| > private static Customer ReadCustomer2()
| > {
| > Customer cust = new Customer("246-12-5645");
| > cust.FirstName = "Robert";
| > cust.LastName = "Brown";
| > return cust;
| > }
| > }
| > public class Customer : IEditableObject
| > {
| >
| > struct CustomerData
| > {
| > internal string id ;
| > internal string firstName ;
| > internal string lastName ;
| > }
| >
| > private CustomersList parent;
| > private CustomerData custData;
| > private CustomerData backupData;
| > private bool inTxn = false;
| >
| > // Implements IEditableObject
| > void IEditableObject.BeginEdit()
| > {
| > Console.WriteLine("Start BeginEdit");
| > if (!inTxn)
| > {
| > this.backupData = custData;
| > inTxn = true;
| > Console.WriteLine("BeginEdit - " + this.backupData.lastName);
| > }
| > Console.WriteLine("End BeginEdit");
| > }
| >
| > void IEditableObject.CancelEdit()
| > {
| > Console.WriteLine("Start CancelEdit");
| > if (inTxn)
| > {
| > this.custData = backupData;
| > inTxn = false;
| > Console.WriteLine("CancelEdit - " + this.custData.lastName);
| > }
| > Console.WriteLine("End CancelEdit");
| > }
| >
| > void IEditableObject.EndEdit()
| > {
| > Console.WriteLine("Start EndEdit" + this.custData.id +
| > this.custData.lastName);
| > if (inTxn)
| > {
| > backupData = new CustomerData();
| > inTxn = false;
| > Console.WriteLine("Done EndEdit - " + this.custData.id +
| > this.custData.lastName);
| > }
| > Console.WriteLine("End EndEdit");
| > }
| >
| > public Customer(string ID) : base()
| > {
| > this.custData = new CustomerData();
| > this.custData.id = ID;
| > this.custData.firstName = "";
| > this.custData.lastName = "";
| > }
| >
| > public string ID
| > {
| > get
| > {
| > return this.custData.id;
| > }
| > }
| >
| > public string FirstName
| > {
| > get
| > {
| > return this.custData.firstName;
| > }
| > set
| > {
| > this.custData.firstName = value;
| > }
| > }
| >
| > public string LastName
| > {
| > get
| > {
| > return this.custData.lastName;
| > }
| > set
| > {
| > this.custData.lastName = value;
| > }
| > }
| >
| > internal CustomersList Parent
| > {
| > get
| > {
| > return parent;
| > }
| > set
| > {
| > parent = value ;
| > }
| > }
| >
| > private void OnCustomerChanged()
| > {
| > if (!inTxn && Parent != null)
| > {
| > Parent.CustomerChanged(this);
| > }
| > }
| > }
| > #endregion
| > }
| >
| > </code>
| >
| >
| >
| >
| > Best regards,
| >
| > Ying-Shen Yu [MSFT]
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| >
| > This posting is provided "AS IS" with no warranties and confers no
rights.
| > You should not reply this mail directly, "Online" should be removed
before
| > sending, Thanks!
| >
| > --------------------
| > | From: "moondaddy" <[email protected]>
| > | Subject: Sync combo box to current record on form
| > | Date: Wed, 15 Oct 2003 16:40:38 -0500
| > | Lines: 19
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: adsl-66-137-118-208.dsl.hstntx.swbell.net
| > 66.137.118.208
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:54528
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | I have a form with unbound controls on it, one of which is a combo
who's
| > | datasource is bound to a dataview. For example, say the combo's name
is
| > | 'Fruit' and it's dataview has 2 columns in it, ID (int32) and Name
| > (string)
| > | and the following rows:
| > | 1 apple
| > | 2 orange
| > | 3 grape
| > |
| > | When the record changes in the business class supporting this form and
| > it's
| > | 'Fruit' property's value changes to 2, how do I get the combo to sync
to
| > the
| > | row who's ID value is 2 (orange) and have 'orange' display in the
text?
| > |
| > | Thanks
| > |
| > |
| > | --
| > | (e-mail address removed)
| > |
| > |
| > |
| >
|
|
|
 
Hi moondaddy,
I must apologize for my premature conclusion, after talking with a guy who
is supporting for ADO.NET, I got to know the behavior of setting a new
value to the SelectedValue is not to change that value but to search for
the value in the bounded column. The documentation didn't make it clear.
So actually, there is a short cut, you may bind the ID property to the
ValueMember ,and set ComboBoxValue.SelectedValue to the ID you want to
choose.
Here is a short code snippet, you may try it to see if it is suitable to
your case.

Sorry for bring you so much inconvience.

<code>
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private DataTable dt;
private void Form1_Load(object sender, System.EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("ID",typeof(string));
dt.Columns.Add("Name",typeof(string));
dt.Rows.Add(new object[]{"a","Apple"});
dt.Rows.Add(new object[]{"b","Orange"});
dt.Rows.Add(new object[]{"c","Grape"});

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "ID";
}

private void button1_Click(object sender, System.EventArgs e)
{
comboBox1.SelectedValue =textBox1.Text;
}
</code>


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Yes! Yes! Yes! Now that's the silver bullet I've been looking for. THANKS
for your persistence!
 
Back
Top