一:创建类GridCheckMarksSelection  

        #region Fileds
RepositoryItemGridLookUpEdit _currentRepository; protected ArrayList selection;
protected String checkColumnFieldName = "CheckMarkSelection";
RepositoryItemCheckEdit edit;
const Int32 CheckboxIndent = ;
#endregion #region Construct
public GridCheckMarksSelection(RepositoryItemGridLookUpEdit repository)
: this()
{
CurrentRepository = repository;
} public RepositoryItemGridLookUpEdit CurrentRepository
{
get { return _currentRepository; }
set
{
if (_currentRepository != value)
{
Detach();
Attach(value);
}
}
} public GridCheckMarksSelection()
{
selection = new ArrayList();
this.OnSelectionChanged();
}
#endregion #region Attribute
public ArrayList Selection
{
get { return selection; }
set { selection = value; }
} public Int32 SelectedCount { get { return selection.Count; } }
#endregion #region GridSelect
public object GetSelectedRow(Int32 index)
{ return selection[index]; } public Int32 GetSelectedIndex(object row)
{ return selection.IndexOf(row); } public void ClearSelection(GridView currentView)
{
selection.Clear();
Invalidate(currentView);
OnSelectionChanged();
} public void SelectAll(object sourceObject)
{
selection.Clear();
if (sourceObject != null)
{
if (sourceObject is ICollection)
selection.AddRange(((ICollection)sourceObject));
else
{
GridView currentView = sourceObject as GridView;
for (Int32 i = ; i < currentView.DataRowCount; i++)
selection.Add(currentView.GetRow(i));
Invalidate(currentView);
}
}
this.OnSelectionChanged();
} public delegate void SelectionChangedEventHandler(object sender, EventArgs e);
public event SelectionChangedEventHandler SelectionChanged;
public void OnSelectionChanged()
{
if (SelectionChanged != null)
{
EventArgs e = new EventArgs();
SelectionChanged(this, e);
}
}
public void SelectGroup(GridView currentView, Int32 rowHandle, bool select)
{
if (IsGroupRowSelected(currentView, rowHandle) && select) return;
for (Int32 i = ; i < currentView.GetChildRowCount(rowHandle); i++)
{
Int32 childRowHandle = currentView.GetChildRowHandle(rowHandle, i);
if (currentView.IsGroupRow(childRowHandle))
SelectGroup(currentView, childRowHandle, select);
else
SelectRow(currentView, childRowHandle, select, false);
}
Invalidate(currentView);
} public void SelectRow(GridView currentView, Int32 rowHandle, bool select)
{
SelectRow(currentView, rowHandle, select, true);
} public void InvertRowSelection(GridView currentView, Int32 rowHandle)
{
if (currentView.IsDataRow(rowHandle))
SelectRow(currentView, rowHandle, !IsRowSelected(currentView, rowHandle));
if (currentView.IsGroupRow(rowHandle))
SelectGroup(currentView, rowHandle, !IsGroupRowSelected(currentView, rowHandle));
} public bool IsGroupRowSelected(GridView currentView, Int32 rowHandle)
{
for (Int32 i = ; i < currentView.GetChildRowCount(rowHandle); i++)
{
Int32 row = currentView.GetChildRowHandle(rowHandle, i);
if (currentView.IsGroupRow(row))
{
if (!IsGroupRowSelected(currentView, row)) return false;
}
else
if (!IsRowSelected(currentView, row)) return false;
}
return true;
} public bool IsRowSelected(GridView currentView, Int32 rowHandle)
{
if (currentView.IsGroupRow(rowHandle))
return IsGroupRowSelected(currentView, rowHandle); object row = currentView.GetRow(rowHandle);
return GetSelectedIndex(row) != -;
}
#endregion #region Attach|Detach
protected virtual void Attach(RepositoryItemGridLookUpEdit rep)
{
if (rep == null) return;
selection.Clear();
_currentRepository = rep; edit = _currentRepository.View.GridControl.RepositoryItems.Add("CheckEdit") as RepositoryItemCheckEdit; GridColumn column = _currentRepository.View.Columns.Add();
column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
column.Visible = true;
column.VisibleIndex = ;
column.FieldName = checkColumnFieldName;
column.Caption = "Mark";
column.OptionsColumn.ShowCaption = false;
column.OptionsColumn.AllowEdit = false;
column.OptionsColumn.AllowSize = false;
column.UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
column.Width = GetCheckBoxWidth();
column.ColumnEdit = edit; _currentRepository.View.Click += new EventHandler(View_Click);
_currentRepository.View.CustomDrawColumnHeader += new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
_currentRepository.View.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
_currentRepository.View.CustomUnboundColumnData += new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
_currentRepository.View.KeyDown += new KeyEventHandler(view_KeyDown);
} protected virtual void Detach()
{
if (_currentRepository == null) return;
if (edit != null)
{
_currentRepository.View.GridControl.RepositoryItems.Remove(edit);
edit.Dispose();
}
_currentRepository.View.Click -= new EventHandler(View_Click);
_currentRepository.View.CustomDrawColumnHeader -= new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
_currentRepository.View.CustomDrawGroupRow -= new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
_currentRepository.View.CustomUnboundColumnData -= new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
_currentRepository.View.KeyDown -= new KeyEventHandler(view_KeyDown);
_currentRepository = null;
} void Invalidate(GridView currentView)
{
currentView.BeginUpdate();
currentView.EndUpdate();
}
void SelectRow(GridView currentView, Int32 rowHandle, bool select, bool invalidate)
{
if (IsRowSelected(currentView, rowHandle) == select) return;
object row = currentView.GetRow(rowHandle);
if (select)
selection.Add(row);
else
selection.Remove(row);
if (invalidate)
Invalidate(currentView);
OnSelectionChanged();
}
void view_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
GridView currentView = sender as GridView;
if (e.Column != null && e.Column.FieldName == checkColumnFieldName)
{
if (e.IsGetData)
e.Value = IsRowSelected(currentView, currentView.GetRowHandle(e.ListSourceRowIndex));
else
SelectRow(currentView, currentView.GetRowHandle(e.ListSourceRowIndex), (bool)e.Value);
}
}
void view_KeyDown(object sender, KeyEventArgs e)
{
GridView currentView = sender as GridView;
if (currentView.FocusedColumn.FieldName != checkColumnFieldName || e.KeyCode != Keys.Space) return;
InvertRowSelection(currentView, currentView.FocusedRowHandle);
}
void View_Click(object sender, EventArgs e)
{
GridHitInfo info;
GridView currentView = (sender as GridView);
Point pt = currentView.GridControl.PointToClient(Control.MousePosition);
info = currentView.CalcHitInfo(pt);
if (info.Column != null && info.Column.FieldName == checkColumnFieldName)
{
if (info.InColumn)
{
if (SelectedCount == currentView.DataRowCount)
ClearSelection(currentView);
else
SelectAll(currentView);
}
if (info.InRowCell)
InvertRowSelection(currentView, info.RowHandle);
}
if (info.InRow && currentView.IsGroupRow(info.RowHandle) && info.HitTest != GridHitTest.RowGroupButton)
InvertRowSelection(currentView, info.RowHandle);
}
void View_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column != null && e.Column.FieldName == checkColumnFieldName)
{
e.Info.InnerElements.Clear();
e.Painter.DrawObject(e.Info);
DrawCheckBox(e.Graphics, e.Bounds, SelectedCount == (sender as GridView).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 * - , );
DrawCheckBox(e.Graphics, r, IsGroupRowSelected((sender as GridView), e.RowHandle));
e.Handled = true;
}
#endregion #region CheckBox
protected Int32 GetCheckBoxWidth()
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
Int32 width = ;
GraphicsInfo.Default.AddGraphics(null);
try
{
width = info.CalcBestFit(GraphicsInfo.Default.Graphics).Width;
}
finally
{
GraphicsInfo.Default.ReleaseGraphics();
}
return width + CheckboxIndent * ;
} 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();
}
#endregion

二:实例化该类并调用相关多选方法(gridLookUpDevice为gridLookUpEdit控件名称,dt为gridLookUpDevice绑定的数据源)

//添加CheckBox控件,实现多选
     this.gridLookUpDevice.Properties.View.OptionsSelection.MultiSelect = true;
     this.gridLookUpDevice.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(gridLookUpDevice_CustomDisplayText);

GridCheckMarksSelection gridCheckMarksDevice;

gridCheckMarksDevice = new GridCheckMarksSelection(gridLookUpDevice.Properties);

     gridCheckMarksDevice.SelectionChanged += new GridCheckMarksSelection.SelectionChangedEventHandler(gridCheckMarksDevice_SelectionChanged);

     gridCheckMarksDevice.SelectAll(dt.DefaultView);

     gridLookUpDevice.Properties.Tag = gridCheckMarksDevice;

         private void gridCheckMarksDevice_SelectionChanged(object sender, EventArgs e)
{
if (ActiveControl is GridLookUpEdit)
{
StringBuilder sb = new StringBuilder();
foreach (DataRowView rv in (sender as GridCheckMarksSelection).Selection)
{
if (sb.ToString().Length > ) { sb.Append(", "); }
sb.Append(rv["DeviceName"].ToString());
}
(ActiveControl as GridLookUpEdit).Text = sb.ToString();
}
} private void gridLookUpDevice_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
StringBuilder sb = new StringBuilder();
GridCheckMarksSelection gridCheckMark = sender is GridLookUpEdit ? (sender as GridLookUpEdit).Properties.Tag as GridCheckMarksSelection : (sender as RepositoryItemGridLookUpEdit).Tag as GridCheckMarksSelection;
if (gridCheckMark == null) return;
foreach (DataRowView rv in gridCheckMark.Selection)
{
if (sb.ToString().Length > ) { sb.Append(", "); }
sb.Append(rv["DeviceName"].ToString());
}
e.DisplayText = sb.ToString(); deviceList = SelectAllDeviceCode(sender, e);
m_sender = sender;
} private List<String> SelectAllDeviceCode(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
List<String> strDeviceCodeList = new List<String>(); GridCheckMarksSelection gridCheckMark = sender is GridLookUpEdit ? (sender as GridLookUpEdit).Properties.Tag as GridCheckMarksSelection : (sender as RepositoryItemGridLookUpEdit).Tag as GridCheckMarksSelection;
if (gridCheckMark == null) return null;
foreach (DataRowView rv in gridCheckMark.Selection)
{
String deviceCode = rv["DeviceCode"].ToString();
strDeviceCodeList.Add(deviceCode);
}
return strDeviceCodeList;
}

三:获取GridLookUpEdit选中的集合

        private void ShowBarChart()
{ if (m_sender != null)
{
GridCheckMarksSelection gridCheckMark = m_sender is GridLookUpEdit ? (m_sender as GridLookUpEdit).Properties.Tag as GridCheckMarksSelection : (m_sender as RepositoryItemGridLookUpEdit).Tag as GridCheckMarksSelection;
if (gridCheckMark != null)
{
foreach (DataRowView rowView in gridCheckMark.Selection)
{
String deviceCode = rowView.Row["DeviceCode"].ToString();
String deviceName = rowView.Row["DeviceName"].ToString();
if (!ls.Contains(deviceCode) && !String.IsNullOrWhiteSpace(deviceCode))
{
DataRow row = dt.NewRow();
row["DeviceCode"] = deviceCode;
row["DeviceName"] = deviceName;
row["DeviceCount"] = "";
dt.Rows.Add(row);
}
}
}
}
}

DevExpress gridLookUpEdit 实现多选的更多相关文章

  1. DevExpress XtraTreeList的复选框 禁用

    树的2个事件代码如下,通过节点的tag判断是否禁用节点前的复选框.树的节点加载时设置要禁用的节点tag为-1,不禁用的则设为相关的值 private void treeListPer_CustomDr ...

  2. DevExpress XtraGrid RepositoryItemCheckEdit 复选框多选的解决方法

    1. RepositoryItemCheckEdit默认有三种状态,选中状态.未选中状态和半选中状态(半选中状态通常用在TreeList中如果父节点下的子节点有选中的有未选中的,则父节点状态为半选中状 ...

  3. 实现DataGridView和DevExpress.GridControl表头全选功能

    1)DevExpress控件的GridView的实现多选操作 先讲DevExpress控件的GridView的实现,要实现的功能基本上是处理单击全选操作.重新绘制表头等操作,首先在加载第一步实现相关的 ...

  4. DevExpress内 GridControl中复选框值问题

    在DevExpress的 GridControl内的复选柜勾选后,界面看到是勾选状态,但对应的DataView的值仍未变,在以下事件内处理 在对应的DataView内的 CellValueChangi ...

  5. DevExpress下拉多选框 CheckComboboxEdit、CheckedListBoxControl

    CheckComboboxEdit //清空项            checkedComboBoxEdit1.Properties.Items.Clear(); //自定义数组            ...

  6. DevExpress XtraTreeList TreeList复选框选择

    权限管理涉及复选框多勾选. 1.控件属性设置 TreeList.OperationView.ShowCheckBoxes=true;用于显示CheckBox: TreeList.OperationBe ...

  7. Winform中设置和获取DevExpress的RadioGroup的选中项的value值

    场景 Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...

  8. DevExpress GridView 整理(转)

    DevExpress GridView 那些事儿 1:去除 GridView 头上的 "Drag a column header here to group by that column&q ...

  9. DevExpress GridView 那些事儿

    1:去除 GridView 头上的 "Drag a column header here to group by that column" -->  点击 Run Desig ...

随机推荐

  1. PHP写入文件用file_put_contents代替fwrite优点多多(转)

    使用php有一段时间了,之前一直用fwrite写入文件,不过当我知道file_put_contents这个函数之后,fwrite就比较少用了,file_put_contents比fwrite代码更简洁 ...

  2. hive-通过Java API操作

    通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...

  3. HTTP 缓存策略

    浏览器一般缓存图片.CSS.JS等静态文件,因为这些文件的更新频率相对来说比较低,合理利用浏览器的缓存对网站的性能提升有很大帮助.HTTP缓存分为两部分,分别是本地缓存和缓存协商,当本地缓存不生效时会 ...

  4. cocos2d-x回收池原理

    cocos2d-x源于cocos2d-iphone,为了与Objective-c一致,cocos2d-x也采用了引用计数与自动回收的内存管理机制. 要现实自动内存回收,需继承于cocos2d-x的根类 ...

  5. css笔记16:盒子模型的入门案例

    1.案例一: 效果图如下: (1)box1.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...

  6. Android(java)学习笔记106-2:反射机制

    1.反射机制: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...

  7. jQuery数组处理

    1. $.each(array, [callback]) 遍历[常用] 解释: 1.不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象(不仅仅是数组哦~). 2.回 ...

  8. PHP用反撇号(`,也就是键盘上ESC键下面的那个,和~在同一个上面)执行外部命令

    例如: echo `whoami`; // 导出数据库,要导入的文件夹必须要有可写权限, -u -p之后的内容必须要紧挨着写 echo `mysqldump -h localhost -u$DbUse ...

  9. linux-软连接

    ln -s /opt/lampp/bin/mysql(绝对路径) /usr/local/bin(软连接目录或者文件) 如果不愿意配置环境变脸可以直接创建env查出的路径下建连接

  10. Visual Studio Team Foundation Server 2015(TFS 秘钥、序列号)

    Visual Studio Team Foundation Server 2015 序列号:PTBNK-HVGCM-HB2GW-MXWMH-T3BJQ