新人求助,DEV中checkbox的多选问题!
新人请教:Dev的gridview里面添加了checkbox列,但是不能多选,高手们给个具体的例子,因为新手不咋熟悉,谢谢了!!!
数据源设置一个bit格式 写过的一个通用多记录选择器, 可以通过checkbox多选记录
public partial class frmMulitDataSelector : frmBase
{
GridCheckMarksSelection selection = null;
/// <summary>
/// 返回的选中行
/// </summary>
public DataTable ResultDataTable
{
get;
private set;
}
public frmMulitDataSelector(DataTable dataSource, DataSelectorColumnInfo[] columnInfos = null, bool allowEdit = false)
{
InitializeComponent();
if (dataSource == null) throw new Exception("未设置数据源!");
//初始化GridControl
gc.DataSource = dataSource.Copy();
gv.OptionsBehavior.Editable = allowEdit;
gv.BestFitColumns();
gv.Columns.Clear();
if (columnInfos == null || columnInfos.Length == 0)
{
gv.PopulateColumns();
}
else
{
List<GridColumn> columns = new List<GridColumn>();
foreach (var info in columnInfos)
{
GridColumn col = new GridColumn();
col.Caption = info.Caption;
col.FieldName = info.FieldName;
col.Visible = true;
col.VisibleIndex = info.VisibleIndex;
col.OptionsColumn.AllowEdit = info.AllowEdit;
columns.Add(col);
if (col.OptionsColumn.AllowEdit) continue;
Font f = new System.Drawing.Font(col.AppearanceCell.Font, FontStyle.Bold);
col.AppearanceCell.Font = f;
col.AppearanceCell.Options.UseFont = true;
col.AppearanceCell.ForeColor = Color.DarkGray;// Color.FromArgb(127, 127, 127, 127);
col.AppearanceCell.Options.UseForeColor = true;
}
gv.Columns.AddRange(columns.ToArray());
}
//设置每列的默认Filter为Contains而不是StartWith
for (int i = 0; i < gv.Columns.Count; i++)
{
gv.Columns.OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;
}
selection = new GridCheckMarksSelection(gv);
}
private void gv_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
var cells = gv.GetSelectedCells().Where(row => row.Column == gv.FocusedColumn);
if (cells.Count() > 1)
{
foreach (var cell in cells)
{
selection.SelectRow(cell.RowHandle, true);
}
}
}
private void btOK_Click(object sender, EventArgs e)
{
if (selection.SelectedCount == 0)
{
Msg.ShowInformation("请至少选择一条记录!");
}
else
{
//构造datatable
DataTable dataSource = gc.DataSource as DataTable;
DataTable dtRet = dataSource.Clone();
for (int i = 0; i < selection.SelectedCount; i++)
{
DataRow dr = (selection.GetSelectedRow(i) as DataRowView).Row;
dtRet.ImportRow(dr);
}
ResultDataTable = dtRet;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
private void btnAdjust_Click(object sender, EventArgs e)
{
gv.BestFitColumns();
}
private void btnSelectAll_Click(object sender, EventArgs e)
{
selection.SelectAll();
}
private void btnInvertSelection_Click(object sender, EventArgs e)
{
for(int i = 0;i<gv.DataRowCount;i++)
{
selection.InvertRowSelection(i);
}
}
private void gv_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
{
if (e.MenuType == GridMenuType.Row)
{
if (gv.FocusedColumn != null)
{
var cells = gv.GetSelectedCells().Where(row => row.Column == gv.FocusedColumn);
if (cells.Count() > 1 )
{
DXMenuItem miSelect = new DXMenuItem("选择(&S)",
(pSender,pEventArgs) =>
{
foreach(var cell in cells)
{
selection.SelectRow(cell.RowHandle, true);
}
});
DXMenuItem miUnSelect = new DXMenuItem("取消选择(&U)",
(pSender,pEventArgs)=>
{
foreach (var cell in cells)
{
selection.SelectRow(cell.RowHandle, false);
}
});
e.Menu.Items.Add(miSelect);
e.Menu.Items.Add(miUnSelect);
}
}
//fillzonevalue
var column = gv.FocusedColumn;
if (column.OptionsColumn.AllowEdit)
{
var cells = gv.GetSelectedCells().Where(row => row.Column == column).ToArray();
if (cells.Count() > 1 && gv.GetSelectedCells().GroupBy(row => row.Column).Count() == 1)
{
DXMenuItem miFillCellValue = new DXMenuItem("填充相同值到选中区域(&F)",
(pSender, pEventArgs) =>
{
object fillValue = gv.GetRowCellValue(cells.RowHandle, cells.Column);
for (int i = 1; i < cells.Length; i++)
{
gv.SetRowCellValue(cells.RowHandle, cells.Column, fillValue);
}
});
e.Menu.Items.Add(miFillCellValue);
}
}
}
}
private void gv_RowCellClick(object sender, RowCellClickEventArgs e)
{
if (e.Column != selection.CheckMarkColumn && e.Column.OptionsColumn.AllowEdit == false && gv.GetSelectedCells().Length == 1)
{
selection.InvertRowSelection(e.RowHandle);
}
}
private void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
{
}
}
public class GridCheckMarksSelection
{
protected GridView _view;
protected ArrayList selection;
GridColumn column;
RepositoryItemCheckEdit edit;
const int CheckboxIndent = 4;
public GridCheckMarksSelection()
{
selection = new ArrayList();
}
public GridCheckMarksSelection(GridView view)
: this()
{
View = view;
}
public GridView View
{
get { return _view; }
set
{
if (_view != value)
{
Detach();
Attach(value);
}
}
}
public GridColumn CheckMarkColumn { get { return column; } }
public int SelectedCount { get { return selection.Count; } }
public object GetSelectedRow(int index)
{
return selection;
}
public int GetSelectedIndex(object row)
{
return selection.IndexOf(row);
}
public void ClearSelection()
{
selection.Clear();
Invalidate();
}
public void SelectAll()
{
selection.Clear();
// fast (won't work if the grid is filtered)
//if(_view.DataSource is ICollection)
// selection.AddRange(((ICollection)_view.DataSource));
//else
// slow:
for (int i = 0; i < _view.DataRowCount; i++)
selection.Add(_view.GetRow(i));
Invalidate();
}
public void SelectGroup(int rowHandle, bool select)
{
if (IsGroupRowSelected(rowHandle) && select) return;
for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
{
int childRowHandle = _view.GetChildRowHandle(rowHandle, i);
if (_view.IsGroupRow(childRowHandle))
SelectGroup(childRowHandle, select);
else
SelectRow(childRowHandle, select, false);
}
Invalidate();
}
public void SelectRow(int rowHandle, bool select)
{
SelectRow(rowHandle, select, true);
}
public void InvertRowSelection(int rowHandle)
{
if (View.IsDataRow(rowHandle))
{
SelectRow(rowHandle, !IsRowSelected(rowHandle));
}
if (View.IsGroupRow(rowHandle))
{
SelectGroup(rowHandle, !IsGroupRowSelected(rowHandle));
}
}
public bool IsGroupRowSelected(int rowHandle)
{
for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
{
int row = _view.GetChildRowHandle(rowHandle, i);
if (_view.IsGroupRow(row))
{
if (!IsGroupRowSelected(row)) return false;
}
else
if (!IsRowSelected(row)) return false;
}
return true;
}
public bool IsRowSelected(int rowHandle)
{
if (_view.IsGroupRow(rowHandle))
return IsGroupRowSelected(rowHandle);
object row = _view.GetRow(rowHandle);
return GetSelectedIndex(row) != -1;
}
protected virtual void Attach(GridView view)
{
if (view == null) return;
selection.Clear();
this._view = view;
view.BeginUpdate();
try
{
edit = view.GridControl.RepositoryItems.Add("CheckEdit") as RepositoryItemCheckEdit;
column = view.Columns.Add();
column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
column.Visible = true;
column.VisibleIndex = 0;
column.FieldName = "CheckMarkSelection";
column.Caption = "Mark";
column.OptionsColumn.ShowCaption = false;
column.OptionsColumn.AllowEdit = false;
column.OptionsColumn.AllowSize = false;
column.OptionsFilter.AllowFilter = false;
column.OptionsFilter.AllowAutoFilter = false;
column.UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
column.Width = GetCheckBoxWidth();
column.ColumnEdit = edit;
view.Click += new EventHandler(View_Click);
view.CustomDrawColumnHeader += new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
view.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
view.CustomUnboundColumnData += new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
view.KeyDown += new KeyEventHandler(view_KeyDown);
view.RowStyle += new RowStyleEventHandler(view_RowStyle);
}
finally
{
view.EndUpdate();
}
}
protected virtual void Detach()
{
if (_view == null) return;
if (column != null)
column.Dispose();
if (edit != null)
{
_view.GridControl.RepositoryItems.Remove(edit);
edit.Dispose();
}
_view.Click -= new EventHandler(View_Click);
_view.CustomDrawColumnHeader -= new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
_view.CustomDrawGroupRow -= new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
_view.CustomUnboundColumnData -= new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
_view.KeyDown -= new KeyEventHandler(view_KeyDown);
_view.RowStyle -= new RowStyleEventHandler(view_RowStyle);
_view = null;
}
protected int GetCheckBoxWidth()
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
int width = 0;
GraphicsInfo.Default.AddGraphics(null);
try
{
width = info.CalcBestFit(GraphicsInfo.Default.Graphics).Width;
}
finally
{
GraphicsInfo.Default.ReleaseGraphics();
}
return width + CheckboxIndent * 2;
}
protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked)
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
painter = edit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
info.EditValue = Checked;
info.Bounds = r;
info.CalcViewInfo(g);
args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
void Invalidate()
{
_view.CloseEditor();
_view.BeginUpdate();
_view.EndUpdate();
}
void SelectRow(int rowHandle, bool select, bool invalidate)
{
if (IsRowSelected(rowHandle) == select) return;
object row = _view.GetRow(rowHandle);
if (select)
selection.Add(row);
else
selection.Remove(row);
if (invalidate)
{
Invalidate();
}
}
void view_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
if (e.Column == CheckMarkColumn)
{
if (e.IsGetData)
{
e.Value = IsRowSelected(View.GetRowHandle(e.ListSourceRowIndex));
}
else
SelectRow(View.GetRowHandle(e.ListSourceRowIndex), (bool)e.Value);
}
}
void view_KeyDown(object sender, KeyEventArgs e)
{
if (View.FocusedColumn != column || e.KeyCode != Keys.Space) return;
InvertRowSelection(View.FocusedRowHandle);
}
void View_Click(object sender, EventArgs e)
{
GridHitInfo info;
Point pt = _view.GridControl.PointToClient(Control.MousePosition);
info = _view.CalcHitInfo(pt);
if (info.Column == column)
{
if (info.InColumn)
{
if (SelectedCount == _view.DataRowCount)
ClearSelection();
else
SelectAll();
}
if (info.InRowCell)
{
InvertRowSelection(info.RowHandle);
}
}
if (info.InRow && _view.IsGroupRow(info.RowHandle) && info.HitTest != GridHitTest.RowGroupButton)
{
InvertRowSelection(info.RowHandle);
}
}
void View_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == column)
{
e.Info.InnerElements.Clear();
e.Painter.DrawObject(e.Info);
DrawCheckBox(e.Graphics, e.Bounds, SelectedCount == _view.DataRowCount);
e.Handled = true;
}
}
void View_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo info;
info = e.Info as DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo;
info.GroupText = " " + info.GroupText.TrimStart();
e.Info.Paint.FillRectangle(e.Graphics, e.Appearance.GetBackBrush(e.Cache), e.Bounds);
e.Painter.DrawObject(e.Info);
Rectangle r = info.ButtonBounds;
r.Offset(r.Width + CheckboxIndent * 2 - 1, 0);
DrawCheckBox(e.Graphics, r, IsGroupRowSelected(e.RowHandle));
e.Handled = true;
}
void view_RowStyle(object sender, RowStyleEventArgs e)
{
//if (IsRowSelected(e.RowHandle))
//{
// e.Appearance.BackColor = SystemColors.Highlight;
// e.Appearance.ForeColor = SystemColors.HighlightText;
//}
}
}
其实GridCheckMarksSelection是devexpress官方提供的帮助类, 可以在google里搜索相应的例子
页:
[1]