|
Infralution Support Support groups for Infralution products
|
View previous topic :: View next topic |
Author |
Message |
media-company.biz
Joined: 17 Dec 2006 Posts: 1
|
Posted: Sun Dec 17, 2006 9:49 pm Post subject: How to bind controls to the current selected row? |
|
|
Hi Infralution,
I'm not the first one with this question, but I couldn't find an answer, precise enough to find a better solution on my own: How to bind the currently selected row (or item, is there really a big difference?) to textboxes and other controls?
I've played a little bit with your DatabaseBrowser. I have added Panels below the _virtualtree, showing the one or the other based on which type of row is selected in the _virtualtree. You will find a compilable and partly working example in your mail.
So far so good, but: how to bind the controls on the Panles to the currently selected Recordset? How to find the type of the current row?
I found solutions, where I have to unbound and rebound each control to the current item - each time the user select a new item in the tree. Normaly, this should be done automaticly - but how? The CurrencyManager didn't seems to work with your virtualrecordsets.
Code: | v_NumberPlate.DataBindings.Clear();
v_NumberPlate.DataBindings.Add(new Binding("text",_virtualtree.SelectedItem,"NumberPlate")); |
Is this really the way it should be coded?
I hope, there is an easier way to do it, so please show it to me in the mailed example.
Thanks,
Marc |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Dec 18, 2006 12:18 am Post subject: |
|
|
The problem is that the standard data binding mechanism only supports binding to either a single simple object (property binding) or binding to a single list via a currency manager. In the case of a tree however we typically have multiple lists, the list which represents the root items and then lists of children for each row.
You can use currency manager binding for items in the root list. To do this you setup the bindings in the InitializeComponent methods as follows:
Code: | _vehicles = new VehicleRecordset();
_virtualTree.DataSource = _vehicles;
v_Model.DataBindings.Add(new Binding("text", _vehicles, "Model"));
v_Vehicle.DataBindings.Add(new Binding("text", _vehicles, "NumberPlate")); |
Note I've added a member variable for the VehicleRecordset. Then in the SelectionChanged event you set the currency manager position for the vehicle list as follows:
Code: | private void _virtualTree_SelectionChanged(object sender, EventArgs e)
{
object selectedItem = _virtualTree.SelectedItem;
VehiclePanel.Visible = (selectedItem is Vehicle);
IncidentPanel.Visible = (selectedItem is Incident);
if (selectedItem is Vehicle)
{
this.BindingContext[_vehicles].Position = (_vehicles as IList).IndexOf(selectedItem);
}
else if (selectedItem is Incident)
{
}
} |
If you try this you will see that your text boxes are now bound to the selected item when it is a vehicle. Unfortunately there are multiple Incident lists (one for each vehicle) and so you cannot bind text boxes to any single list using the above mechanism. The only way you can use databinding in this case is to use simple property data binding to bind to the SelectedItem and change the bindings each time the selection changes as your original code did. I'm not sure that databinding isn't more trouble then it is worth in this situation and you may be better off simply handling setting the text box properties manually and handling the textbox validation events (or an Apply button click) to retrieve the data from the text boxes. _________________ Infralution Support |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Dec 18, 2006 12:57 am Post subject: |
|
|
Actually you could kind of fudge using currency manager databinding for the Incident lists by creating a list of all Incidents and binding to this then finding the selected incident in the overall list. So your InitializeComponent method would look like:
Code: | _vehicles = new VehicleRecordset();
_incidents = new IncidentRecordset();
_virtualTree.DataSource = _vehicles;
v_Model.DataBindings.Add(new Binding("text", _vehicles, "Model"));
v_Vehicle.DataBindings.Add(new Binding("text", _vehicles, "NumberPlate"));
i_Date.DataBindings.Add(new Binding("text", _incidents, "Date"));
i_Location.DataBindings.Add(new Binding("text", _incidents, "Location"));
|
And your SelectionChanged handler would be:
Code: | private void _virtualTree_SelectionChanged(object sender, EventArgs e)
{
object selectedItem = _virtualTree.SelectedItem;
VehiclePanel.Visible = (selectedItem is Vehicle);
IncidentPanel.Visible = (selectedItem is Incident);
if (selectedItem is Vehicle)
{
this.BindingContext[_vehicles].Position = (_vehicles as IList).IndexOf(selectedItem);
}
else if (selectedItem is Incident)
{
this.BindingContext[_incidents].Position = _incidents.FindUsingPrimaryKey((selectedItem as Incident).ID);
}
} |
While this maybe somewhat neater than creating new bindings each time it actually doesn't seem to perform as well (probably due to the cost of locating the selected record the in IncidentRecordset. Because the vehicle record is actually a member of the _vehicles recordset we can use the IndexOf method which performs significantly better than searching by primary key. _________________ Infralution Support |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|