我有一个DataGrid控件在我的窗体上,我需要在运行时向它添加一行。 我有一个DataGrid列,需要在其中添加不同的UI控件(每个单元格都包含不同的UI控件,例如下拉框、复选框、超链接、单选按钮)。我可以添加其他控件,但无法添加单选按钮控件,如何将单选按钮添加到DataGrid列中? 我尝试使用这里的方法,但对于我来说并没有起作用,因为它要求整个列都是单选按钮列。
我有一个DataGrid控件在我的窗体上,我需要在运行时向它添加一行。 我有一个DataGrid列,需要在其中添加不同的UI控件(每个单元格都包含不同的UI控件,例如下拉框、复选框、超链接、单选按钮)。我可以添加其他控件,但无法添加单选按钮控件,如何将单选按钮添加到DataGrid列中? 我尝试使用这里的方法,但对于我来说并没有起作用,因为它要求整个列都是单选按钮列。
这里是如何将复选框列作为单选按钮的方法。从您的图片中可以看出,您需要一行所有列都作为单选按钮 -> 这更加复杂,取决于您如何将数据绑定到 datagridview。我更多地使用 VB.NET,所以这里可能会有一些语法错误...
在您的 datagridview 中创建一个 DataGridViewCheckBoxColumn 列 通过三个 DataGridView 事件,我的复选框列可以作为单选按钮工作
private Boolean bRbtnCurrentValue
private Int32 iColumnRadioBtn = 4
private Datagridview dgv //Only for this exapmle
//In Event dgv_CellBeginEdit
{
//Here we stored current value to variable
if (this.dgv.CurrentCell.ColumnIndex = this.iColumnRadioBtn)
this.bRbtnCurrentValue = this.dgv
}
//In Event dgv_CellEndEdit
{
//Here we update if value changed
Boolean bNewValue = this.dgv.CurrentCell.Value
if (this.dgv.CurrentCell.ColumnIndex = this.iColumnRadioBtn)
{
if(bNewValue=False)
this.dgv.CurrentCell.Value=this.bRbtnCurrentValue
else
//Here jo actions when value changed(database update etc.)
}
}
//Event dgv_ CurrentCellDirtyStateChanged
{
if(this.dgv.CurrentCell.ColumnIndex = this.iColumnRadioBtn) andalso (dgv.IsCurrentCellDirty = True)
{
foreach(DataGridViewRow dgvr In dgv.Rows)
{
If (dgvr.Index = dgv.CurrentRow.Index)
If (dgv.CurrentCell.Value = True)
dgv.CancelEdit() //True to False cannot be changed
else
If (dgvr.Cells(dgv.CurrentCell.ColumnIndex).Value=True)
dgvr.Cells(dgv.CurrentCell.ColumnIndex).Value = False
}
}
}