protected override void OnMouseDown(MouseEventArgs e) { var pindex = GetIndexAtPoint(new Point(e.X, e.Y)); var index = GetIndex(pindex); if (GetImageBounds(pindex).Contains(e.X, e.Y) && index < Folder.PartIndexs.Count) { if ((ModifierKeys & Keys.Control) == Keys.Control) { if (e.Button == MouseButtons.Left) { if (SelectedIndexs.Contains(index)) { SelectedIndexs.Remove(index); SelectedItemsChanged(this, null); Invalidate(index); } else { SelectedIndexs.Add(index); SelectedItemsChanged(this, null); Invalidate(index); } } } else { if (!SelectedIndexs.Contains(index)) { SelectedIndexs.Clear(); SelectedIndexs.Add(index); SelectedItemsChanged(this, null); Invalidate(); } if (e.Button == MouseButtons.Right) { contextMenuStrip1.Show(this, new Point(e.X, e.Y)); } } } else { SelectedIndexs.Clear(); SelectedItemsChanged(this, null); Invalidate(); if (e.Button == MouseButtons.Right) { contextMenuStrip1.Show(this, e.X, e.Y); } } base.OnMouseDown(e); }
/// <summary> /// 根据选中行,读取记录表中的行号和数据,排除多选的情况 /// </summary> /// <param name="popupEvent"></param> private GridViewPopupEventArgs SetPopupEvent(int keyIndex) { GridViewPopupEventArgs popupEvent = new GridViewPopupEventArgs(this); switch (dataType) { case DataEnum.DataTable: case DataEnum.DataRows: { var item = currentPageRowSource.Skip(keyIndex - 1).FirstOrDefault(); popupEvent.RowIndex = _dataRowSource.IndexOf(item); popupEvent.RowSource = item; } break; case DataEnum.BaseModel: { var item = currentPageModelSource.Skip(keyIndex - 1).FirstOrDefault(); popupEvent.RowIndex = _dataModelSource.IndexOf(item); popupEvent.RowSource = item; } break; default: break; } if (!MultiSelect) //单选 { SelectedIndexs.Clear(); SelectedIndexs.Add(popupEvent.RowIndex); } else //多选 { //不存在时,选中状态,否则无响应 if (!SelectedIndexs.Contains(popupEvent.RowIndex)) { SelectedIndexs.Add(popupEvent.RowIndex); } else { SelectedIndexs.Remove(popupEvent.RowIndex); } } popupEvent.SelectedIndexs = SelectedIndexs; return(popupEvent); }