using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace DelftTools.Controls.Swf { /// /// TODO: this class name seems to be very non-intuitive, find a better name, discuss design. /// GridBasedDialog implements a simple form with2 grids with a master slave relation /// The slave grid is optional. /// Typically the grids are bound to a datasource. The caller is responsible for responding /// to selection changes. /// public partial class GridBasedDialog : Form { /// /// This event is raised when the selection in the master grid changes /// public event EventHandler MasterSelectionChanged; /// /// This event is raised when the selection in the slave grid changes /// public event EventHandler SlaveSelectionChanged; /// /// Default contructor for the form /// public GridBasedDialog() { InitializeComponent(); dataGridViewMaster.SelectionChanged += MasterDataGridViewSelectionChanged; dataGridViewSlave.SelectionChanged += SlaveDataGridViewSelectionChanged; } /// /// Set to true to support multiple selection in the master grid; default is false. /// public bool MasterMultiSelect { get { return dataGridViewMaster.MultiSelect; } set { dataGridViewMaster.MultiSelect = value; } } /// /// Set to true to support multiple selection in the slave grid; default is false. /// public bool SlaveMultiSelect { get { return dataGridViewSlave.MultiSelect; } set { dataGridViewSlave.MultiSelect = value; } } /// /// The text used to name the master grid in the form /// public string MasterTitle { get { return groupBoxMaster.Text; } set { groupBoxMaster.Text = value; } } /// /// The text used to name the slave grid in the form /// public string SlaveTitle { get { return groupBoxSlave.Text; } set { groupBoxSlave.Text = value; } } /// /// Set to true if doubleclick in a grid should close the form with DialogResult.OK if there is /// valid selection. /// public bool MouseDoubleClickIsOk { get; set; } /// /// Set to true to only use the master grid /// public bool SingleList { get { return splitContainer1.Panel2Collapsed; } set { splitContainer1.Panel2Collapsed = value; } } /// /// Set this property to the objects to be displayed in the master grid. /// public IEnumerable MasterDataSource { get { return (IEnumerable) dataGridViewMaster.DataSource; } set { dataGridViewMaster.DataSource = value; } } /// /// Set this property to the objects to be displayed in the slave grid. /// Typically the caller of the form set this property when the selection /// in the master grid changes. /// public IEnumerable SlaveDataSource { get { return (IEnumerable) dataGridViewSlave.DataSource; } set { dataGridViewSlave.DataSource = value; } } /// /// The indices of the rows that are selected in the master grid /// public IEnumerable MasterSelectedIndices { get { return DataGridViewSelectedIndices(dataGridViewMaster); } } /// /// The indices of the rows that are selected in the slave grid /// public IEnumerable SlaveSelectedIndices { get { return DataGridViewSelectedIndices(dataGridViewSlave); } } public void SetMasterSelectedIndices(IEnumerable pSelectedIndices) { var selectedIndices = pSelectedIndices as int[] ?? pSelectedIndices.ToArray(); foreach (DataGridViewRow row in dataGridViewMaster.Rows) { row.Selected = selectedIndices.Contains(row.Index); } } private void MasterDataGridViewSelectionChanged(object sender, EventArgs e) { if (null != MasterSelectionChanged) { MasterSelectionChanged(this, e); } } private void SlaveDataGridViewSelectionChanged(object sender, EventArgs e) { if (null != SlaveSelectionChanged) { SlaveSelectionChanged(this, e); } } private static IEnumerable DataGridViewSelectedIndices(DataGridView dataGridView) { IList selectedIndices = new List(); foreach (DataGridViewRow s in dataGridView.SelectedRows) { selectedIndices.Add(dataGridView.Rows.IndexOf(s)); } return selectedIndices; } private void dataGridViewSlave_MouseDoubleClick(object sender, MouseEventArgs e) { if ((!MouseDoubleClickIsOk) || (!MasterSelectedIndices.Any())) { return; } if ((!SingleList) && ((!SlaveSelectedIndices.Any()))) { return; } DialogResult = DialogResult.OK; Close(); } } }