Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Adding a record to a dataset
Message
De
11/01/2005 10:57:35
 
 
À
11/01/2005 10:27:46
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
00975570
Message ID:
00976055
Vues:
15
Kevin,

We have a DataBind method in all our classes. We pass the table and columnname of the what we're binding to. I haven't tried binding a checkbox to a property, but I imagine the FormatHandler will still work. Here's some sample code.

m_BoundTable and m_BoundColumn are fields that are exposed as
public properties, so they can be set in the Property sheet if
you wish.
public virtual void DataBind(System.Data.DataTable Data, string Path)
{
	for (int i = 0; i < this.DataBindings.Count; i++)
	{
		if (this.DataBindings[i].Equals(this.oBinding))
		{
			this.DataBindings.Remove(this.oBinding);
			break;
		}
	}

	this.Text = "";
	this.m_BoundTable = Data;
	this.m_BoundColumn = Path;
	this.oBinding = new Binding(this.m_BoundProperty, Data, Path);
	this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
	this.oBinding.Parse += new ConvertEventHandler(this.ParseHandler);
	this.DataBindings.Add(this.oBinding);
}
DataBinding with a Form property:
You need to create an actual property with get/set (just making a field public will not work).
You also need a Changed eventhandler, because the DataBinding listens for this event (this is to take care of the situation where you change the property programmatically and need it to be reflected in the bound control).
private string m_MyProperty;

public event EventHandler MyPropertyChanged
public string MyProperty
{
  get {return m_MyProperty;}
  set 
  {
    m_MyProperty = value;
    if (MyPropertyChanged != null)
      MyPropertyChanged(this, new EventArgs());
  }
}

...

MyTextBox.DataBindings.Add("Text", this, "MyProperty");
HTH,
~~Bonnie



>>In your checkbox class, you need an Event Handler for the Format event of the Binding:
>>
>>Binding oBind = new Binding("Checked", MyTable, MyColumn);
>>oBind.Format += new ConvertEventHandler(this.FormatHandler);
>>
>>protected virtual void FormatHandler(object sender, ConvertEventArgs e)
>>{
>>	if (e.Value == System.DBNull.Value)
>>		e.Value = false;
>>}
>>
>
>TVM.
>
>Where do MyTable and MyColumn come from, bearing in mind that this should be generic?
>
>Also, what if my data is represented by properties of an object, rather than bound directly to a table?
>
>TIA, Kevin
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform