I want to use my DataGridView
only to show things, and I want the user not to be able to select any row, field or anything from the DataGridView
.
我想仅使用我的DataGridView来显示内容,我希望用户不能从DataGridView中选择任何行,字段或任何内容。
How can I do this?
我怎样才能做到这一点?
8 个解决方案
#1
96
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 DataGridView
should 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.
我不同意没有DataGridView应该是不可选择的广泛断言。一些用户界面是为工具或触摸屏而构建的,允许选择误导用户认为选择实际上会将它们带到某个地方。
Setting ReadOnly = true
on 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.
另一种选择是将控件选择的颜色设置为与未选择的颜色完全相同的颜色,但如果您碰巧操纵单元格的背面颜色,则此方法也会产生一些令人讨厌的结果。
#2
11
You may set a transparent background color for the selected cells as following:
您可以为所选单元格设置透明背景颜色,如下所示:
DataGridView.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
#3
4
Enabled
property to false
启用属性为false
or
要么
this.dataGridView1.DefaultCellStyle.SelectionBackColor = this.dataGridView1.DefaultCellStyle.BackColor;
this.dataGridView1.DefaultCellStyle.SelectionForeColor = this.dataGridView1.DefaultCellStyle.ForeColor;
#4
2
I fixed this by setting the Enabled
property to false
.
我通过将Enabled属性设置为false来修复此问题。
#5
1
I found setting all AllowUser...
properties to false
, ReadOnly
to true
, RowHeadersVisible
to false
, ScollBars
to None
, then faking the prevention of selection worked best for me. Not setting Enabled
to false
still allows the user to copy the data from the grid.
我发现将所有AllowUser ...属性设置为false,将ReadOnly设置为true,将RowHeadersVisible设置为false,将ScollBars设置为None,然后假冒选择最适合我。未将Enabled设置为false仍允许用户从网格中复制数据。
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);
#6
1
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(适当的重载);)
#7
1
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;
#8
0
Use the DataGridView.ReadOnly
property
使用DataGridView.ReadOnly属性
The code in the MSDN example illustrates the use of this property in a DataGridView
control 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;
#1
96
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 DataGridView
should 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.
我不同意没有DataGridView应该是不可选择的广泛断言。一些用户界面是为工具或触摸屏而构建的,允许选择误导用户认为选择实际上会将它们带到某个地方。
Setting ReadOnly = true
on 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.
另一种选择是将控件选择的颜色设置为与未选择的颜色完全相同的颜色,但如果您碰巧操纵单元格的背面颜色,则此方法也会产生一些令人讨厌的结果。
#2
11
You may set a transparent background color for the selected cells as following:
您可以为所选单元格设置透明背景颜色,如下所示:
DataGridView.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
#3
4
Enabled
property to false
启用属性为false
or
要么
this.dataGridView1.DefaultCellStyle.SelectionBackColor = this.dataGridView1.DefaultCellStyle.BackColor;
this.dataGridView1.DefaultCellStyle.SelectionForeColor = this.dataGridView1.DefaultCellStyle.ForeColor;
#4
2
I fixed this by setting the Enabled
property to false
.
我通过将Enabled属性设置为false来修复此问题。
#5
1
I found setting all AllowUser...
properties to false
, ReadOnly
to true
, RowHeadersVisible
to false
, ScollBars
to None
, then faking the prevention of selection worked best for me. Not setting Enabled
to false
still allows the user to copy the data from the grid.
我发现将所有AllowUser ...属性设置为false,将ReadOnly设置为true,将RowHeadersVisible设置为false,将ScollBars设置为None,然后假冒选择最适合我。未将Enabled设置为false仍允许用户从网格中复制数据。
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);
#6
1
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(适当的重载);)
#7
1
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;
#8
0
Use the DataGridView.ReadOnly
property
使用DataGridView.ReadOnly属性
The code in the MSDN example illustrates the use of this property in a DataGridView
control 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;