IDE:Visual Studio c#、Winforms 应用程序。
我投入了大约12个小时,但没有取得成功。作为DataGridView
不提供单选按钮类型的单元格。所以我尝试使用复选框单元格作为单选按钮功能。
即我只想选中一列中的一个复选框。
看图片:
这看起来很简单,但相信我,它并不像我们想象的那么简单。在回复之前请测试代码。
这是我尝试过的示例测试代码:
code 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 上测试
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;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//clean al rows
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells["Select"].Value = false;
}
//check select row
dataGridView1.CurrentRow.Cells["Select"].Value = true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)