DataGridView:如何启用多行选择但禁用多单元格选择?
我正在寻找一种在 DataGridView-Control 中启用多行选择但禁用多单元格选择的方法。
到目前为止我已经尝试过:
- DataGridView.MultiSelect = true 允许
- 在 DataGridView_CellMouseClick-Event 中选择多行和单元格 ClearSelection() 并重新选择最后选定的单元格看起来不太好(您会看到旧单元格取消选择,然后新的单元格选择;SuspendLayout() 和 ResumeLayout() 没有帮助)
- DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect 不是一个选项:如果用户单击一个单元格,则应该仅选择该单元格
它用于导出功能:用户应该能够将选定的行导出到文件,但一般来说,他不应该能够选择多个单元格(用于复制和粘贴等)。
问候,
inno
----- [更新] -----
这是我的实现。工作正常(为了紧凑而删除注释):
using System.Windows.Forms;
namespace YourAmazingNamespace
{
public partial class SpecialSelectDataGridView: DataGridView
{
public SpecialSelectDataGridView()
{
InitializeComponent();
}
protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
{
ResetSelectedCells();
base.SetSelectedCellCore(columnIndex, rowIndex, selected);
}
void ResetSelectedCells()
{
foreach (DataGridViewCell cell in SelectedCells) {
base.SetSelectedCellCore(cell.ColumnIndex, cell.RowIndex, false);
}
}
}
}
通过 MultiSelect = true(默认值)选择多行,并且在选择新单元格之前通过调用 ResetSelectedCells() 重置当前选定的单元格。
HTH,谢谢和问候,
inno
I'm looking for a way to enable multi row select in a DataGridView-Control but disable multi cell select.
What I've tried so far:
- DataGridView.MultiSelect = true allows to select multi rows and cells
- ClearSelection() in DataGridView_CellMouseClick-Event and re-select the last selected cell doesn't look very nice (you see the old cell deselect and then the new cell select; SuspendLayout() and ResumeLayout() doesn't help)
- DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect is not an option: If the user clicks a cell, it should only this cell get selected
It's for an export function: The user should be able to export selected rows to a file but in general he should not be able to select more than one cell (for copy & paste etc.).
Regards,
inno
----- [UPDATE] -----
Here's my implementation. Works fine (comments removed for compactness):
using System.Windows.Forms;
namespace YourAmazingNamespace
{
public partial class SpecialSelectDataGridView: DataGridView
{
public SpecialSelectDataGridView()
{
InitializeComponent();
}
protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
{
ResetSelectedCells();
base.SetSelectedCellCore(columnIndex, rowIndex, selected);
}
void ResetSelectedCells()
{
foreach (DataGridViewCell cell in SelectedCells) {
base.SetSelectedCellCore(cell.ColumnIndex, cell.RowIndex, false);
}
}
}
}
Multiple rows are selected through MultiSelect = true (default value) and currently selected cells are resetted by calling ResetSelectedCells() before selecting the new one.
HTH, thanks and regards,
inno
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以覆盖 SetSelectedRowCore 或 SetSelectedCelCore 并执行自定义选择。
MSDN 引用:
当然,这意味着您将必须使用派生数据网格而不是标准数据网格。
You could override SetSelectedRowCore or SetSelectedCelCore and perform your custom selection.
MSDN Quote:
Of course this means you will have to use an derived datagrid and not the standard one.