Building the Task DetailViewer form

The TaskDetailViewer form takes in a Task object; you will need to write code to bind the controls of this form to the properties of the Task object.

You can use a DateTimePicker control for the Due On field (which is the date and time the task is due), a Combobox control (with some preloaded data in the Items property) for the Status field, and text boxes for the Subject and Description fields. Drag a BindingSource control to the form, and name it TaskBindingSource. The binding source control allows you to manage the editing lifecycle of the data bound controls. Finally, add the Save and Cancel menu buttons to allow the user to either save his or her changes or to discard them.

The following is the code you have to write for this form. In the constructor of the form, call the SetupBindings() method to setup the data binding. The Save and Cancel menu buttons will call the EndEdit() and CancelEdit() methods of the TaskBindingSource control respectively to either save or discard the user's changes.

using System;

using System.Windows.Forms;

namespace CRMLive {

public partial class TaskDetailViewer {

private Task _Datasource;

private void SetupBindings() {

this.TaskBindingSource.DataSource = typeof(Task); this.TaskBindingSource.Add(_Datasource);

dtpDueOn.DataBindings.Add(new Binding("Value", TaskBindingSource, "TaskDate", true));

txtSubject.DataBindings.Add(new Binding("Text", TaskBindingSource, "TaskSubject", true));

txtDescription.DataBindings.Add(new Binding("Text", TaskBindingSource, "TaskDescription", true));


Binding("SelectedIndex", TaskBindingSource, "TaskStatus", true));

dtpDueOn.Value = _Datasource.TaskDate;

public void btnSave_Click(System.Object sender, System.EventArgs e)

this.TaskBindingSource.EndEdit(); this.DialogResult =

System.Windows.Forms.DialogResult.OK; this.Close();

public void btnCancel_Click(System.Object sender, System.EventArgs e)

this.TaskBindingSource.CancelEdit(); this.DialogResult =

System.Windows.Forms.DialogResult.Cancel; this.Close();

public TaskDetailViewer(Task Datasource) {

// This call is required by the Windows Form //Designer.


// Add any initialization after the //InitializeComponent() call.

_Datasource = Datasource; SetupBindings();

Was this article helpful?

0 0

Post a comment