在datagridview中如何使用复选框作为单选按钮? [英] In datagridview how to use checkbox as radiobutton?
问题描述
IDE:Visual Studio c#,Winforms应用程序.
IDE: Visual Studio c#, Winforms Application.
我已经投入了大约12个小时,但没有成功.由于 DataGridView
不提供单选按钮类型的单元格.所以我试图将复选框单元格用作无线电广播功能.
I have invested around 12 hours but didn't get success. As DataGridView
don't provide radiobutton type of cell. so i am trying to use checkbox cell as radio-buttion functionality.
即,我只想选中一列中的一个复选框.
i.e I want to be checked only one checkbox in a column.
查看图片:
这看起来很简单,但是请相信我,这并不像我们想的那么简单.在给出答复之前,请先测试代码.
It looks very simple thing but trust me it is not as simple as we are thinking. before giving reply please test the code.
这是我尝试过的示例测试代码:
Here are my sample tested code which i have tried:
代码1
////start
if (e.RowIndex != -1)
{
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && dataGridView1.CurrentCell.ColumnIndex == 0) //null check
{
if (e.ColumnIndex == 0)
{
if (((bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value == true))
{
for (int k = 0; k <= 4; k++)
{
//uncheck all other checkboxes but keep the current as checked
if (k == dataGridView1.CurrentRow.Index)
{
dataGridView1.Rows[k].Cells[0].Value = false;
}
//if (gvTeam1.Rows[k].Cells[2].Selected != null)
//if(k !=e.RowIndex)
}
// gvTeam1.Rows[e.RowIndex].Cells[2].Value = false; // keep the current captain box checked
}
}
//}
// gvTeam1.Rows[rowPointerTeam1].Cells[2].Value = true;
}
}
//end
// here gvTeam1 is Datagridview1
代码2:在datagridview1上测试
code 2: tested on datagridview1
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
for (int i = 0; i < 8; i++)
{
//if (i != dataGridView1.CurrentCell.RowIndex)
dataGridView1.Rows[i].Cells[0].Value = false;
}
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value = true;
}
}
推荐答案
我知道聚会晚了,但这是使用实际单选按钮而不是复选框的代码.
I know I'm late to the party, but here is code to use actual radio buttons instead of checkboxes.
信用:
感谢Arsalan Tamiz的博客文章,该代码基于该文章.
http://arsalantamiz.blogspot.com/2008/09/using-datagridview-checkbox-column-as.html
也要感谢M.毒蛇的答案奠定了一些基础工作.
Credit:
Thanks to Arsalan Tamiz's blog post, on which this code is based.
http://arsalantamiz.blogspot.com/2008/09/using-datagridview-checkbox-column-as.html
Also thanks to M. Viper's answer for laying some of the ground work.
步骤1:将DataGridView控件添加到面板并添加CheckBoxColumn(我分别将其命名为mine grid
和 colRadioButton
).我不确定这是否重要,但是我的DataGridView的EditMode属性设置为 EditOnEnter
.
Step 1: Add a DataGridView control to your panel and add a CheckBoxColumn (I named mine grid
and colRadioButton
respectively). I'm not sure if this matters, but the EditMode property of my DataGridView is set to EditOnEnter
.
步骤2:为CellPainting事件创建事件处理程序.
Step 2: Create an event handler for the CellPainting event.
private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == colRadioButton.Index && e.RowIndex >= 0)
{
e.PaintBackground(e.ClipBounds, true);
// TODO: The radio button flickers on mouse over.
// I tried setting DoubleBuffered on the parent panel, but the flickering persists.
// If someone figures out how to resolve this, please leave a comment.
Rectangle rectRadioButton = new Rectangle();
// TODO: Would be nice to not use magic numbers here.
rectRadioButton.Width = 14;
rectRadioButton.Height = 14;
rectRadioButton.X = e.CellBounds.X + (e.CellBounds.Width - rectRadioButton.Width) / 2;
rectRadioButton.Y = e.CellBounds.Y + (e.CellBounds.Height - rectRadioButton.Height) / 2;
ButtonState buttonState;
if (e.Value == DBNull.Value || (bool)(e.Value) == false)
{
buttonState = ButtonState.Normal;
}
else
{
buttonState = ButtonState.Checked;
}
ControlPaint.DrawRadioButton(e.Graphics, rectRadioButton, buttonState);
e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);
e.Handled = true;
}
}
步骤3:处理CurrentCellDirtyStateChanged事件以取消选中先前的选择.这基本上与 M相同.毒蛇的答案.
Step 3: Handle the CurrentCellDirtyStateChanged event to uncheck the previous selection. This is basically the same as M. Viper's answer.
private void radioButtonChanged()
{
if (grid.CurrentCell.ColumnIndex == colRadioButton.Index)
{
foreach (DataGridViewRow row in grid.Rows)
{
// Make sure not to uncheck the radio button the user just clicked.
if (row.Index != grid.CurrentCell.RowIndex)
{
row.Cells[colRadioButton.Index].Value = false;
}
}
}
}
private void grid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
radioButtonChanged();
}
第4步:(可选)处理CellClick事件,以允许用户通过单击单元格中的任意位置而不是仅直接单击单选按钮来检查单选按钮.
Step 4: (Optional) Handle the CellClick event to allow the user to check the radio button by clicking anywhere in the cell rather than only directly on the radio button.
private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == colRadioButton.Index)
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)grid.Rows[e.RowIndex].Cells[colRadioButton.Index];
cell.Value = true;
radioButtonChanged();
}
}
这篇关于在datagridview中如何使用复选框作为单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!