.net - validacion - Habilitar y deshabilitar una celda en una vista de cuadrícula de datos
ocultar formulas en excel sin proteger hoja (3)
Puede configurar una fila o celda particular para que sea de solo lectura, por lo que el usuario no puede cambiar el valor. ¿Es eso lo que quieres decir?
dataGridView1.Rows[0].ReadOnly = true;
dataGridView1.Rows[1].Cells[2].ReadOnly = true;
Estoy usando un control DataGridView para mostrar algunos datos. Necesito habilitar algunos datos y desactivar algunos datos de forma dinámica en función de algunos valores en la grilla.
¿Alguien puede decirme cómo hacerlo?
Para "deshabilitar" una celda, debe ser de solo lectura y atenuada de alguna manera. Esta función activa / desactiva un 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;
}
}
Paso 1:
type form load : -
For i = 0 to Datagridview.columns.count -1
if i <> 1 then //restricted columns, ''i'' is Your column index
Datagridview.Columns(i).ReadOnly = True
end if
Next
Paso 2
type Cellbeginedit
Datagridview.BeginEdit(True)