DataGridView的 - 我怎样才能使一个复选框充当一个单选按钮? [英] DataGridView - how can I make a checkbox act as a radio button?

查看:379
本文介绍了DataGridView的 - 我怎样才能使一个复选框充当一个单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序显示在的 DataGridView中

I have a Windows Forms app that displays a list of objects in a DataGridView.

这控件呈现布尔值复选框。

This control renders bool values as checkboxes.

有一组的是相互排斥的对象属性三个复选框。至多其中之一可以是真实的。因此我想复选框来像一组单选按钮。

There's a set of three checkboxes in the object properties that are mutually exclusive. At most one of them can be true. Accordingly I'd like the checkboxes to act like a set of radio buttons.

刚刚从老家伙一个侧面一句话:我觉得人这几天甚至不知道为什么这些被称为单选按钮。昔日在汽车收音机有4个或5个按钮,按下和其中任何一个引起的一切他人弹出。他们是互相排斥的。 单选按钮可能不是一个有用的信息,因为这些天的收音机没有这样的按钮了,我不认为。

Just a side remark from the old guy: I think people these days don't even know why these are called radio buttons. In the old days a radio in a car had 4 or 5 buttons, and depressing any one of them caused all the others to pop out. They were mutually exclusive. "Radio button" is probably not a helpful description these days because radios don't have buttons like that anymore, I don't think.

我该怎么办呢?我想如果我附上的CheckedChanged事件的复选框,我知道该行,我就能找到所有其他复选框。

How can I do it? I figure if I attach a "CheckedChanged" event to the checkboxes, and I know the row, I will be able find all the other checkboxes.

什么事件可以挂钩我抢当它第一次呈现CheckBox控件,这样我就可以了的CheckedChanged事件附加到了吗?我知道 DataGridView.CellFormatting ,但我认为这是错误的,因为它被调用每一个DataGridView的油漆时间。我真的需要被称为只有DGV呈现第一次的事件。

What event can I hook to grab the checkbox control when it is first rendered, so that I Can attach the CheckedChanged event to it? I know about DataGridView.CellFormatting, but I think that's wrong because it gets called every time the DataGridView paints. I really need an event that is called only the first time the DGV is rendered.

推荐答案

你想要的是CellContentClick,上在DGV本身。安装,检查,看是否DGV的那列是CheckBoxCell的处理程序,如果是这样,取消该行的所有其他复选框。

The one you want is CellContentClick, on the DGV itself. Attach a handler that checks to see if that column of the DGV is a CheckBoxCell, and if so, uncheck all other checkboxes on the row.

刚一说明,虽然,对于一个CheckBoxCell,此事件触发的复选框值实际上改变之前。这意味着,无论你做对当前单元格的东西,它会通过后触发事件覆盖。将摇出的这种行为是可以有没有检查,通过检查该行一个盒子,然后再检查它在一行细胞的你(无论是尝试在处理程序设置复选框值与否,复选框将结束第二次点击后清零)。为了克服这一点,力量要检查的复选框之一,可以处理CellValueChanged相反,如果改变了细胞是当前单元和未被选中,检查它。

Just a note though, for a CheckBoxCell, this event fires before the checkbox value actually changes. This means that regardless of what you do to the current cell, it will be overridden by events that fire later. The behavior that will shake out of this is that you can have none of the cells on a row checked, by checking one box on the row and then checking it again (whether you try to set the checkbox value in the handler or not, the checkbox will end up cleared after the second click). To overcome that and force one of the checkboxes to be checked, you can handle CellValueChanged instead, and if the cell that changed is the current cell and is unchecked, check it.

这篇关于DataGridView的 - 我怎样才能使一个复选框充当一个单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆