切换DataGridView行选择,其中SelectionMode为FullRowSelect [英] Toggle DataGridView row selection where SelectionMode is FullRowSelect
问题描述
我有一个DataGridView,其中SelectionMode = FullRowSelect和MultiSelect = False。
I have a DataGridView where SelectionMode=FullRowSelect and MultiSelect=False.
当用户点击一行时,按预期方式进行选择。但是,再次单击相同的行并不会取消选择该行。
When a user clicks on a row it is selected as expected. However, clicking on the same row again does not deselect the row.
如何进行行选择以在选定和未选择之间切换?
How can the row selection be made to toggle between selected and unselected?
推荐答案
据我所知,没有开箱即用的功能。
As far as I know there is no out of the box functionality that will do this.
我设法使用以下代码获得您要求的效果:
I managed to get the effect you are asking for with the following code:
public partial class Form1 : Form
{
private bool selectionChanged;
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (!selectionChanged)
{
dataGridView1.ClearSelection();
selectionChanged = true;
}
else
{
selectionChanged = false;
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
selectionChanged = true;
}
}
使用dataGridView的SelectionChanged和CellClick事件,以及持有选择状态的类级别变量。
That uses the dataGridView's SelectionChanged and CellClick events, along with a class level variable holding the state of the selection.
这篇关于切换DataGridView行选择,其中SelectionMode为FullRowSelect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!