取消选择 datagridview 中的所有行
我有两个 datagridview,当我单击其中一个时,我想取消选择第二个 datagridview 中的所有选择,我尝试了此操作,但没有任何效果:
firstItemsDataGridView.ClearSelection();
firstItemsDataGridView.CurrentCell = null;
不工作,
firstItemsDataGridView.ClearSelection();
if (firstItemsDataGridView.Rows.Count > 0)
firstItemsDataGridView[1, 0].Selected = true;
firstItemsDataGridView.CurrentCell = null;
firstItemsDataGridView.ClearSelection();
foreach (DataGridViewRow item in firstItemsDataGridView.Rows) {
item.Selected = false;
foreach (DataGridViewCell itemCell in firstItemsDataGridView.Columns) {
itemCell.Selected = false;
}
}
不工作,
firstItemsDataGridView.Rows[0,-1].Selected = true;
也不工作。
我已将选择模式设置为全行选择,但我不知道如何实现我的目标。
非常感谢!
I have two datagridviews, and when I click to one of them, I would like to deselect all selection in the second datagridview, I tried this, but nothing works:
firstItemsDataGridView.ClearSelection();
firstItemsDataGridView.CurrentCell = null;
not working,
firstItemsDataGridView.ClearSelection();
if (firstItemsDataGridView.Rows.Count > 0)
firstItemsDataGridView[1, 0].Selected = true;
firstItemsDataGridView.CurrentCell = null;
firstItemsDataGridView.ClearSelection();
foreach (DataGridViewRow item in firstItemsDataGridView.Rows) {
item.Selected = false;
foreach (DataGridViewCell itemCell in firstItemsDataGridView.Columns) {
itemCell.Selected = false;
}
}
not working,
firstItemsDataGridView.Rows[0,-1].Selected = true;
not working too.
I have set selecting mode to full row selection, and I have no idea how to achieve my goal.
thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
应该有效。也许您有自动选择被触发的行的代码?
Should work. Maybe you have code that auto selects rows which is triggered?
NullSkull 的一个答案为我解决了这个问题,即 datagridview 控件的第 0 行、第 0 列的单元格始终在表单加载时被选中,并且尽管在表单的构造函数方法中调用了 ClearSelection,但无法取消选择。我只需在 Form_Load 事件中调用 ClearSelection 即可。
An answer at NullSkull solved the problem for me which was that the cell at row 0, column 0 for the datagridview control was always selected on form load, and could not be deselected despite calling ClearSelection within the form's constructor method. I just had to call ClearSelection in the Form_Load event.
http://www.nullskull.com/q/10066166/how-to-deselect-the-first-row-when-the-form-i-loaded-in-datagridview-in-windows-application.aspx
由于没有答案,并且我在其他帖子中使用了这个答案,并且我遇到了选择第一行并且无法取消选择的相同问题:
这是一个小技巧,您可以看到选择行的唯一方法,位于第一列(不是列 [0],而是因此的列)。当您单击另一行时,您将不再看到蓝色选择,只有箭头指示已选择哪一行。
解决方案:
我发现了为什么我的第一行被默认选择,并找到了如何默认不选择它。
默认情况下,我的 datagridview 是 Windows 窗体上第一个制表位的对象。首先在另一个对象上设置制表位(也许完全禁用数据网格的制表位会起作用)禁用选择第一行
Since there is no answer, and I used this answer in other posts and i ran into the same issue of first row being selected and deselecting wasn't possible:
This is a small trick, the only way you can see a row is selected, is by the very first column (not column[0], but the one therefore). When you click another row, you will not see the blue selection anymore, only the arrow indicates which row have selected.
SOLUTION:
i found out why my first row was default selected and found out how to not select it by default.
By default my datagridview was the object with the first tab-stop on my windows form. Making the tab stop first on another object (maybe disabling tabstop for the datagrid at all will work to) disabled selecting the first row
只需使用:
dgvName.ClearSelection();
Just use:
dgvName.ClearSelection();
我尝试过这个,它对我有用:
I tried this and it's working for me:
确保所有行都被取消选择 (dataGridView.Rows[...].Selected = false)
第 0 行默认为选中,因此在打开 DataGridView 时设置 dataGridView.Rows[0].Selected = false 以及其他选项设置为用户无法选择,那么您将不会选择任何内容。
Make sure all the rows are deselected (dataGridView.Rows[...].Selected = false)
Row zero defaults to selected, so set dataGridView.Rows[0].Selected = false when opening the DataGridView and as long as the other options are set so the user can't select, then you will have, and maintain, nothing selected.
如果您使用 WPF 并希望维护 MVVM 设计模式,您可以将两个选定的项目绑定到同一属性,这样当您选择其中一个时,另一个将自动取消选择。
尝试这样的
*我只使用了单例类,因为这将在不同实例中的不同视图中工作,如果您在同一视图中,则可以使用常规类。
请注意 xmal 上的 IsAsync=True ,如果您计划在不同视图中使用单例类,没有它它将无法工作。
If your using WPF and want to maintain a MVVM design pattern you can bind both selected items to the same property so when you select one the other will automatically be deselected.
try something like this
*I only used a singleton class because this will work across different views in different instances, you can use a regular class if your in the same view.
Please note the IsAsync=True on the xmal, if you plan on using a singleton class across diffrent views it will not work without it.
正如 @Taw 在最佳答案的子评论中所说 - “不要太早打电话!”。
就我而言,默认行为根本不起作用。我的案例 - tabControl 选项卡中的数据网格,如果该选项卡之前未显示,则它不起作用!
这个黑客就像一个魅力:
结果:向用户显示第二个选项卡,当他单击第一个选项卡时,选择将不会出现。
As said @Taw in subcomments to top answer - "Don't call it too soon!".
In my case default behavior not works at all. My case - datagrid in tab of tabControl and it did not work if that tab not shown before!
That hack works like a charm :
As a result : second tab displayed to user, and when he clicked to first - selection will not appear.