我需要启用/禁用datagridview的行列,这可以通过在绑定后循环所有行来轻松完成。 但我想在数据绑定时这样做...有没有办法做到这一点?我如何启用/禁用行单元格?
dgvLayout.AutoGenerateColumns = false;
dgvLayout.DataSource = list;
单击单击但它不起作用
if ((dgvLayout.Rows[e.RowIndex].Cells["colControlText"].Value.ToString()) == "-Invalid-")
{
if (e.ColumnIndex == 2 || e.ColumnIndex == 5)
{
return;
}
else if (e.ColumnIndex == 1)
{
return;
}
}
答案 0 :(得分:2)
您可以在datagrid的RowsAdded事件上编写代码
答案 1 :(得分:2)
您可以使用此解决方案启用和禁用单元格
要“禁用”一个单元格,它必须是只读的并以某种方式变灰。此函数启用/禁用DataGridViewCell:
/// <summary>
/// Toggles the "enabled" status of a cell in a DataGridView. There is no native
/// support for disabling a cell, hence the need for this method. The disabled state
/// means that the cell is read-only and grayed out.
/// </summary>
/// <param name="dc">Cell to enable/disable</param>
/// <param name="enabled">Whether the cell is enabled or disabled</param>
private void enableCell(DataGridViewCell dc, bool enabled) {
//toggle read-only state
dc.ReadOnly = !enabled;
if (enabled)
{
//restore cell style to the default value
dc.Style.BackColor = dc.OwningColumn.DefaultCellStyle.BackColor;
dc.Style.ForeColor = dc.OwningColumn.DefaultCellStyle.ForeColor;
}
else {
//gray out the cell
dc.Style.BackColor = Color.LightGray;
dc.Style.ForeColor = Color.DarkGray;
}
}