我需要启用/禁用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;
}
}发布于 2011-10-21 04:01:55
您可以使用此解决方案启用和禁用单元格
要“禁用”一个单元格,它必须是只读的,并且以某种方式灰显。此函数用于启用/禁用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;
}
}发布于 2011-10-20 20:24:51
你可以在datagrid的RowsAdded事件上编写你的代码
https://stackoverflow.com/questions/7834862
复制相似问题