如果行不可编辑,则更改 DataGridView 列中的图标编辑
Change Icon Edit in DataGridView Column If The Row is not Editable
我以编程方式在 datagridview 中创建了列。首先是编辑列,键列,值列,是可编辑列(隐藏)如下代码所示:
private void InitGrid()
{
DataGridViewImageColumn editColumn = new DataGridViewImageColumn();
editColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
editColumn.HeaderText = "";
editColumn.Image = global::WindowsForms.Properties.Resources.edit;
editColumn.Name = Global.ACTION_EDIT;
editColumn.Width = 30;
DataGridViewTextBoxColumn keyColumn = new DataGridViewTextBoxColumn();
keyColumn.DataPropertyName = "Key";
keyColumn.HeaderText = "Key";
keyColumn.Name = "Key";
DataGridViewTextBoxColumn valueColumn = new DataGridViewTextBoxColumn();
valueColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
valueColumn.DataPropertyName = "Value";
valueColumn.HeaderText = "Value";
valueColumn.Name = "Value";
DataGridViewTextBoxColumn isEditableColumn = new DataGridViewTextBoxColumn();
isEditableColumn.DataPropertyName = "IsEditable";
isEditableColumn.HeaderText = "IsEditable";
isEditableColumn.Name = "IsEditable";
isEditableColumn.Visible = false;
dgvConfiguration.Columns.AddRange(new DataGridViewColumn[] { editColumn,
keyColumn, valueColumn, isEditableColumn
});
}
如果 isEditableColumn
的值为 false,我想显示 disabled_edit.png
图标或空单元格。如何解决这个问题?
您可以使用 DataGridView.CellFormatting 当单元格内容需要格式化以显示时发生的事件。
VB.NET
中的伪代码(抱歉):
If e.ColumnIndex = -1 Or e.RowIndex = -1 Then Exit Sub
With Me.YourDataGridView
If .Columns(e.ColumnIndex).Name = "YourDataGridViewImageColumnName" Then
If .Rows(e.RowIndex).Cells("IsEditable").Value Then
e.Value = <code to get edit.png>
Else
e.Value = <code to get disable_edit.png>
End If
End If
End With
我以编程方式在 datagridview 中创建了列。首先是编辑列,键列,值列,是可编辑列(隐藏)如下代码所示:
private void InitGrid()
{
DataGridViewImageColumn editColumn = new DataGridViewImageColumn();
editColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
editColumn.HeaderText = "";
editColumn.Image = global::WindowsForms.Properties.Resources.edit;
editColumn.Name = Global.ACTION_EDIT;
editColumn.Width = 30;
DataGridViewTextBoxColumn keyColumn = new DataGridViewTextBoxColumn();
keyColumn.DataPropertyName = "Key";
keyColumn.HeaderText = "Key";
keyColumn.Name = "Key";
DataGridViewTextBoxColumn valueColumn = new DataGridViewTextBoxColumn();
valueColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
valueColumn.DataPropertyName = "Value";
valueColumn.HeaderText = "Value";
valueColumn.Name = "Value";
DataGridViewTextBoxColumn isEditableColumn = new DataGridViewTextBoxColumn();
isEditableColumn.DataPropertyName = "IsEditable";
isEditableColumn.HeaderText = "IsEditable";
isEditableColumn.Name = "IsEditable";
isEditableColumn.Visible = false;
dgvConfiguration.Columns.AddRange(new DataGridViewColumn[] { editColumn,
keyColumn, valueColumn, isEditableColumn
});
}
如果 isEditableColumn
的值为 false,我想显示 disabled_edit.png
图标或空单元格。如何解决这个问题?
您可以使用 DataGridView.CellFormatting 当单元格内容需要格式化以显示时发生的事件。
VB.NET
中的伪代码(抱歉):
If e.ColumnIndex = -1 Or e.RowIndex = -1 Then Exit Sub
With Me.YourDataGridView
If .Columns(e.ColumnIndex).Name = "YourDataGridViewImageColumnName" Then
If .Rows(e.RowIndex).Cells("IsEditable").Value Then
e.Value = <code to get edit.png>
Else
e.Value = <code to get disable_edit.png>
End If
End If
End With