【发布时间】:2012-07-04 13:32:50
【问题描述】:
我只想使用我的DataGridView 来显示内容,并且我希望用户不能从DataGridView 中选择任何行、字段或任何内容。
我该怎么做?
【问题讨论】:
-
无法选择是非常糟糕的用户界面设计(对用户来说非常烦人)。如果用户想从您的报告中复制某些内容怎么办?我认为只读就足够了(如下面的答案所述)。
标签: c# .net winforms datagridview
我只想使用我的DataGridView 来显示内容,并且我希望用户不能从DataGridView 中选择任何行、字段或任何内容。
我该怎么做?
【问题讨论】:
标签: c# .net winforms datagridview
我会选择这个:
private void myDataGridView_SelectionChanged(Object sender, EventArgs e)
{
dgvSomeDataGridView.ClearSelection();
}
我不同意没有DataGridView 应该是不可选择的广泛断言。一些 UI 是为工具或触摸屏而构建的,允许选择会误导用户认为选择实际上会将它们带到某个地方。
在控件上设置ReadOnly = true 对是否可以选择单元格或行没有影响。设置Enabled = false 存在视觉和功能上的缺点。
另一种选择是将控件选择的颜色设置为与未选择的颜色完全相同,但如果您碰巧正在操作单元格的背景颜色,那么这种方法也会产生一些令人讨厌的结果。
【讨论】:
您可以为选定的单元格设置透明背景颜色,如下所示:
DataGridView.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
【讨论】:
Transparent 颜色!
Enabled 属性到false
或
this.dataGridView1.DefaultCellStyle.SelectionBackColor = this.dataGridView1.DefaultCellStyle.BackColor;
this.dataGridView1.DefaultCellStyle.SelectionForeColor = this.dataGridView1.DefaultCellStyle.ForeColor;
【讨论】:
我通过将Enabled 属性设置为false 来解决此问题。
【讨论】:
如果您不需要使用所选单元格中的信息,则清除选择有效,但如果您仍需要使用所选单元格中的信息,您可以这样做以使其看起来没有选择和背景颜色仍然可见。
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
dataGridView.RowsDefaultCellStyle.SelectionBackColor = row.DefaultCellStyle.BackColor;
}
}
【讨论】:
这对我来说就像一个魅力:
row.DataGridView.Enabled = false;
row.DefaultCellStyle.BackColor = Color.LightGray;
row.DefaultCellStyle.ForeColor = Color.DarkGray;
(其中 row = DataGridView.NewRow(适当的重载);)
【讨论】:
我发现将所有AllowUser... 属性设置为false、ReadOnly 到true、RowHeadersVisible 到false、ScollBars 到None,然后faking the prevention of selection 最适合我。不将Enabled 设置为false 仍然允许用户从网格中复制数据。
当您想要一个简单的显示网格(假设行的高度相同)时,以下代码也会清理外观:
int width = 0;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
width += dataGridView1.Columns[i].Width;
}
dataGridView1.Width = width;
dataGridView1.Height = dataGridView1.Rows[0].Height*(dataGridView1.Rows.Count+1);
【讨论】:
理论上我最喜欢 user4101525 的回答,但实际上并没有用。 选择不是叠加层,因此您可以看到控制范围内的任何内容
Ramgy Borja 的回答没有处理默认样式实际上根本不是颜色的事实,因此应用它无济于事。 如果应用您自己的颜色(这可能是 edhubbell 所说的讨厌的结果),这会处理默认样式并有效
dgv.RowsDefaultCellStyle.SelectionBackColor = dgv.RowsDefaultCellStyle.BackColor.IsEmpty ? System.Drawing.Color.White : dgv.RowsDefaultCellStyle.BackColor;
dgv.RowsDefaultCellStyle.SelectionForeColor = dgv.RowsDefaultCellStyle.ForeColor.IsEmpty ? System.Drawing.Color.Black : dgv.RowsDefaultCellStyle.ForeColor;
【讨论】:
你必须创建一个自定义的 DataGridView
`
namespace System.Windows.Forms
{
class MyDataGridView : DataGridView
{
public bool PreventUserClick = false;
public MyDataGridView()
{
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (PreventUserClick) return;
base.OnMouseDown(e);
}
}
}
` 请注意,您必须先使用添加的类编译一次程序,然后才能使用新控件。
然后转到 .Designer.cs 并将旧的 DataGridView 更改为新的,而不必弄乱您以前的代码。
private System.Windows.Forms.DataGridView dgv; // found close to the bottom
…
private void InitializeComponent() {
...
this.dgv = new System.Windows.Forms.DataGridView();
...
}
到(各自)
private System.Windows.Forms.MyDataGridView dgv;
this.dgv = new System.Windows.Forms.MyDataGridView();
【讨论】:
它在 VB 中,但应该不难翻译成 C#:
如果要锁定datagridview,请使用dg.ReadOnly == True;
如果您想阻止用户选择另一行,只需记住旧选择并根据条件集或不设置应选择的行。假设,多选已关闭:
Private Sub dg_SelectionChanged(sender As Object, e As EventArgs) Handles dg.SelectionChanged
Static OldSelection As Integer
If dg.Rows.Count > 0 Then
If dg.SelectedRows(0).Index <> OldSelection And YOURCONDITION Then
dg.Rows(OldSelection).Selected = True
End If
OldSelection = dg.SelectedRows(0).Index
End If
End Sub
【讨论】:
在从 DataGridView 继承的类中禁用默认选择对我来说一直有效:
// REQUIRES: SelectionMode = DataGridViewSelectionMode.FullRowSelect
protected override void SetSelectedRowCore(int rowIndex, bool selected)
{
base.SetSelectedRowCore(rowIndex, selected && ALLOW_DEFAULT_SELECTION);
}
bool ALLOW_DEFAULT_SELECTION = false;
通常目标是完全禁用它(以实现我们自己的自定义选择和绘制过程)。当目标是仅在特定时间允许默认选择时,可以像这样包装布尔值:
public void SelectRowExplicitly(int index, bool selected = true)
{
try
{
ALLOW_DEFAULT_SELECTION = true;
Rows[index].Selected = selected;
}
finally
{
ALLOW_DEFAULT_SELECTION = false;
}
}
【讨论】:
使用DataGridView.ReadOnly property
MSDN example 中的代码说明了此属性在DataGridView 控件中的使用主要用于显示。在此示例中,控件的视觉外观以多种方式进行了自定义,并且控件被配置为有限的交互性。
在示例代码中观察这些设置:
// Set property values appropriate for read-only
// display and limited interactivity
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToOrderColumns = true;
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.RowHeadersWidthSizeMode =
DataGridViewRowHeadersWidthSizeMode.DisableResizing;
【讨论】: