C# 如何禁用在 DataGridView 中选择的能力?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11330147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:27:22  来源:igfitidea点击:

How to disable the ability to select in a DataGridView?

c#.netwinformsdatagridview

提问by Mahdi Tahsildari

I want to use my DataGridViewonly to show things, and I want the user not to be able to select any row, field or anything from the DataGridView.

我想使用我的DataGridViewonly 来显示内容,并且我希望用户不能从DataGridView.

How can I do this?

我怎样才能做到这一点?

回答by Angshuman Agarwal

Use the DataGridView.ReadOnlyproperty

使用DataGridView.ReadOnly物业

The code in the MSDN exampleillustrates the use of this property in a DataGridViewcontrol intended primarily for display. In this example, the visual appearance of the control is customized in several ways and the control is configured for limited interactivity.

MSDN 示例中的代码说明了该属性在主要用于显示DataGridView控件中的使用。在此示例中,控件的视觉外观以多种方式自定义,并且控件配置为有限的交互性

Observe these settings in the sample code:

观察示例代码中的这些设置:

// 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;

回答by Rick

I fixed this by setting the Enabledproperty to false.

我通过将Enabled属性设置为false.

回答by edhubbell

I'd go with this:

我会这样做:

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

I don't agree with the broad assertion that no DataGridViewshould be unselectable. Some UIs are built for tools or touchsreens, and allowing a selection misleads the user to think that selecting will actually get them somewhere.

我不同意 noDataGridView应该是不可选择的广泛断言。一些 UI 是为工具或触摸屏构建的,允许选择会误导用户认为选择实际上会将它们带到某个地方。

Setting ReadOnly = trueon the control has no impact on whether a cell or row can be selected. And there are visual and functional downsides to setting Enabled = false.

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

Another option is to set the control selected colors to be exactly what the non-selected colors are, but if you happen to be manipulating the back color of the cell, then this method yields some nasty results as well.

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

回答by SharpC

I found setting all AllowUser...properties to false, ReadOnlyto true, RowHeadersVisibleto false, ScollBarsto None, then faking the prevention of selectionworked best for me. Not setting Enabledto falsestill allows the user to copy the data from the grid.

我发现将所有AllowUser...属性设置为false, ReadOnlyto true, RowHeadersVisibleto false, ScollBarsto None,然后假装防止选择对我来说效果最好。不设置Enabledfalse仍然允许用户从网格复制数据。

The following code also cleans up the look when you want a simple display grid (assuming rows are the same height):

当您想要一个简单的显示网格(假设行的高度相同)时,以下代码还清理了外观:

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);

回答by user4101525

You may set a transparent background color for the selected cells as following:

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

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

回答by Paddy

This worked for me like a charm:

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

row.DataGridView.Enabled = false;

row.DefaultCellStyle.BackColor = Color.LightGray;

row.DefaultCellStyle.ForeColor = Color.DarkGray;

(where row = DataGridView.NewRow(appropriate overloads);)

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

回答by Ramgy Borja

Enabledproperty to false

Enabled财产到 false

or

或者

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

回答by Bengineer

I liked user4101525's answer best in theory but it doesn't actually work. Selection is not an overlay so you see whatever is under the control

我在理论上最喜欢 user4101525 的回答,但实际上并没有用。选择不是叠加层,因此您可以看到受控制的任何内容

Ramgy Borja's answer doesn't deal with the fact that default style is not actually a color at all so applying it doesn't help. This handles the default style and works if applying your own colors (which may be what edhubbell refers to as nasty results)

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;

回答by Jannik Svensson

you have to create a custom DataGridView

你必须创建一个自定义的 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);
        }
    }
}

` note that you have to first compile the program once with the added class, before you can use the new control.

` 请注意,您必须先使用添加的类编译程序一次,然后才能使用新控件。

then go to The .Designer.cs and change the old DataGridView to the new one without having to mess up you previous code.

然后转到 The .Designer.cs 并将旧的 DataGridView 更改为新的,而不必弄乱您以前的代码。

private System.Windows.Forms.DataGridView dgv; // found close to the bottom

private void InitializeComponent() {
    ...
    this.dgv = new System.Windows.Forms.DataGridView();
    ...
}

to (respective)

到(各自)

private System.Windows.Forms.MyDataGridView dgv;

this.dgv = new System.Windows.Forms.MyDataGridView();