如何禁用 DataGridView 中的选择功能?

问题描述 投票:0回答:12

我只想使用

DataGridView
来显示内容,并且我希望用户无法从
DataGridView
中选择任何行、字段或任何内容。

我该怎么做?

c# .net winforms datagridview
12个回答
147
投票

我会选择这个:

private void myDataGridView_SelectionChanged(Object sender, EventArgs e)
{
     myDataGridView.ClearSelection();  
}

我不同意没有

DataGridView
不应该是不可选择的广泛主张。有些 UI 是为工具或触摸屏而构建的,允许选择会误导用户,让他们认为选择实际上会将他们带到某个地方。

在控件上设置

ReadOnly = true
对于是否可以选择单元格或行没有影响。设置
Enabled = false
有视觉和功能上的缺点。

另一种选择是将控件选定的颜色设置为与未选定的颜色完全相同的颜色,但如果您碰巧操纵单元格的背景颜色,那么此方法也会产生一些令人讨厌的结果。


21
投票

您可以为选定的单元格设置透明背景颜色,如下所示:

DataGridView.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;

15
投票

Enabled
属性为
false

this.dataGridView1.DefaultCellStyle.SelectionBackColor = this.dataGridView1.DefaultCellStyle.BackColor;
this.dataGridView1.DefaultCellStyle.SelectionForeColor = this.dataGridView1.DefaultCellStyle.ForeColor;

3
投票

我通过将

Enabled
属性设置为
false
来修复此问题。


3
投票

如果您不需要使用所选单元格中的信息,则可以清除选择,但如果您仍需要使用所选单元格中的信息,您可以执行此操作,使其看起来没有选择,并且背景色仍将可见。

private void dataGridView_SelectionChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView.SelectedRows)
        {
            dataGridView.RowsDefaultCellStyle.SelectionBackColor = row.DefaultCellStyle.BackColor;
        }
    }

2
投票

这对我来说就像一个魅力:

row.DataGridView.Enabled = false;

row.DefaultCellStyle.BackColor = Color.LightGray;

row.DefaultCellStyle.ForeColor = Color.DarkGray;

(其中 row = DataGridView.NewRow(适当的重载);)


2
投票

您必须创建一个自定义 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();

1
投票

我发现将所有

AllowUser...
属性设置为
false
ReadOnly
true
RowHeadersVisible
false
ScollBars
None
,然后 伪造防止选择 对我来说效果最好。不将
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);

1
投票

以下是我一直以来在从 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;
    }
}

0
投票

理论上我最喜欢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;

0
投票

它是用 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

-3
投票

使用

DataGridView.ReadOnly
属性

MSDN 示例中的代码说明了在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;
    
© www.soinside.com 2019 - 2024. All rights reserved.