/// <summary>
/// 设置GridView单选
/// </summary>
/// <param name="view"></param>
/// <param name="selectCaption"></param>
public static void SetGridViewSingleSelect(DevExpress.XtraGrid.Views.Grid.GridView view,string selectCaption="选择")
{
if (view == null) return;
//设置显示复选框列
view.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
view.OptionsSelection.MultiSelect = true;
view.OptionsSelection.CheckBoxSelectorColumnWidth = 45;
view.OptionsSelection.ShowCheckBoxSelectorInColumnHeader = DevExpress.Utils.DefaultBoolean.False;//列标题不显示CheckBox,控制不能全选
//
var selectCol=view.VisibleColumns[0];
if(selectCol.FieldName== "DX$CheckboxSelectorColumn")
{
selectCol.Caption = selectCaption;
}
view.SelectionChanged += onSelectChanged;
void onSelectChanged(object sender , DevExpress.Data.SelectionChangedEventArgs e)
{
//注销事件,防止以下操作反复触发该事件
view.SelectionChanged -= onSelectChanged;
//清除所有选择
view.ClearSelection();
if (e.Action == CollectionChangeAction.Add)//选中
{
view.SelectRow(e.ControllerRow);
}
else if (e.Action == CollectionChangeAction.Remove)//取消选中
{
view.UnselectRow(e.ControllerRow);
}
view.SelectionChanged += onSelectChanged;
}
}