/// Multiselection datagrid.
/// Based on Dino Esposito code.
///
/// MSDN January 2002
/// Copyright (c) 2002 Alexis Grandemange
/// Mail: alexis.grandemange@pagebox.net
/// This program is free software; you can redistribute it and/or
/// modify it under the terms of the GNU Lesser General Public
/// License as published by the Free Software Foundation; version
/// 2.1 of the License.
/// This library is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Lesser General Public License for more details.
/// A copy of the GNU Lesser General Public License lesser.txt should be
/// included in the distribution.
///
public class MultiGrid : DataGrid
{
// Constructor that sets some styles and graphical properties
public MultiGrid()
{
// Set event handlers
Init += new EventHandler(OnInit);
}
// PROPERTY: SelectedItems
public ArrayList SelectedItems
{
get
{
ArrayList a = new ArrayList();
int i = 0;
foreach(DataGridItem dgi in Items)
{
CheckBox cb = (CheckBox)dgi.Cells[0].Controls[0];
a.Add(new SelectedEntry(i++, cb.Checked));
}
return a;
}
}
// EVENT HANDLER: Init
private void OnInit(Object sender, EventArgs e)
{
// Add a templated column that would allow for selection.
// The item template contains a checkbox.
AddSelectColumn();
}
private void AddSelectColumn()
{
// Create the new templated column
TemplateColumn tc = new TemplateColumn();
tc.ItemStyle.BackColor = Color.SkyBlue;
tc.ItemTemplate = new SelectColumnTemplate();
Columns.AddAt(0, tc);
}
}
public class SelectColumnTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
CheckBox cb = new CheckBox();
cb.DataBinding += new EventHandler(BindName);
container.Controls.Add(cb);
}
void BindName(Object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
DataGridItem dgi = (DataGridItem)cb.NamingContainer;
SelectorEntry se = (SelectorEntry)dgi.DataItem;
bool selected = se.Selected;
if (selected)
cb.Checked = true;
else
cb.Checked = false;
}
}
///